[英]Javascript date format issue to string
When I convert from date to String, my date is changing. 当我从日期转换为String时,我的日期正在更改。 What is the my fault?
我的错是什么
Date is returned me: 日期返回给我:
Date 2016-10-31T22:00:00.000Z
And when I convert to String I get: 当我转换为String时,我得到:
Thu Dec 01 2016 00:00:00 GMT+0200
My code: 我的代码:
date = new Date("2016 11 31");
StringDate = date.toString();
console.log(StringDate);
console.log(date);
You're after UTC time, which can be printed with Date.toUTCString()
: 您在UTC时间之后,可以使用
Date.toUTCString()
打印:
var date = new Date("2016 11 31");
var stringDate = date.toUTCString(); // See this line
console.log(stringDate);
console.log(date);
Actually your input is wrong.There is no such date 2016-11-31
. 实际上您输入的是错误的。没有这样的日期
2016-11-31
。 Thats why it shows Dec 01 2016. 这就是为什么它显示2016年12月1日。
Try this: 尝试这个:
date=new Date("2016 10 31"); //november consist 30 days only
StringDate=date.toString();
console.log(StringDate);
console.log(date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.