How do I create the time now
and format it to a string with format of "2014-08-04T17:19:00-07:00"? Using moment.js or any other JavaScript?
如何创建现在的时间并将其格式化为格式为“2014-08-04T17:19:00-07:00”的字符串?使用moment.js或任何其他JavaScript?
I will also need to create 2 new, one to subtract a week from now, one to subtract a month from now, but also with this format.
我还需要创建2个新的,一个从现在开始减去一个,从现在开始减去一个月,但也使用这种格式。
2 个解决方案
#1
0
Simply enough:
var d = Date.now(); //For current time in MS
d = new Date(); //For current time wrapped in the object.
d.toISOString();
For more on ISO String read this or that.
有关ISO String的更多信息,请阅读此内容。
If you want to do any Date arithmetic, like adding a month, just add/subtract the number of milliseconds for that unit to your date object.
如果要进行任何日期算术,例如添加月份,只需将该单位的毫秒数加/减到日期对象即可。
Additionally, if you don't want time in Zulu, you can use String manipulation to drop the Z and add the proper zone. Don't forget to account for the difference in time zones!
此外,如果您不想在Zulu中有时间,可以使用字符串操作来删除Z并添加适当的区域。不要忘记考虑时区的差异!
#2
0
You can do this:
你可以这样做:
var date = new Date();
date.toISOString();
> "2014-08-05T17:22:08.030Z"
// Subtract one week:
var before = date;
before.setDate(date.getDate()-7);
before.toISOString();
> "2014-07-29T17:22:08.030Z"
#1
0
Simply enough:
var d = Date.now(); //For current time in MS
d = new Date(); //For current time wrapped in the object.
d.toISOString();
For more on ISO String read this or that.
有关ISO String的更多信息,请阅读此内容。
If you want to do any Date arithmetic, like adding a month, just add/subtract the number of milliseconds for that unit to your date object.
如果要进行任何日期算术,例如添加月份,只需将该单位的毫秒数加/减到日期对象即可。
Additionally, if you don't want time in Zulu, you can use String manipulation to drop the Z and add the proper zone. Don't forget to account for the difference in time zones!
此外,如果您不想在Zulu中有时间,可以使用字符串操作来删除Z并添加适当的区域。不要忘记考虑时区的差异!
#2
0
You can do this:
你可以这样做:
var date = new Date();
date.toISOString();
> "2014-08-05T17:22:08.030Z"
// Subtract one week:
var before = date;
before.setDate(date.getDate()-7);
before.toISOString();
> "2014-07-29T17:22:08.030Z"