2015-08-28 25 views
-3

我有一個日期對象"2015-10-23T23:59:57",調用getDate(),它返回24。 我很困惑。我爲什麼得到24?當我有一個日期「2015-10-23T23:59:57」

腳本

var d = new Date("2015-10-23T23:59:57"); 
 
var day = d.getDate(); 
 
alert(day);

+2

你能提供的使用方法? – SaviNuclear

+3

'new Date('2015-10-23T23:59:57')'將創建UTC日期,根據您的時區,它必須是10月24日 – Satpal

+0

作爲@Satpal的觀點的例證,'new Date('2015-10- 23T23:59:57')'在我的系統中返回'Sat Oct 24 2015 08:59:57 GMT + 0900(JST)',這堅決地在24日。 – Amadan

回答

0

new Date('2015-10-23T23:59:57')創建UTC日期。根據你的時區,它一定是24th Oct因此你得到24

如果要使用字符串創建日期,可以使用momentjs庫。

var cdt = moment('2015-10-23T23:59:57').toDate(); 
 
alert(cdt.getDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

+0

謝謝!感謝大家。 –

相關問題