Javascript 日期格式化 Fri Jun 21 00:00:00 UTC+0100 2013 to iso 8601 (2013-06-21)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16918846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript date formatting Fri Jun 21 00:00:00 UTC+0100 2013 to iso 8601 (2013-06-21)
提问by Joshua Holden
Please advise:
请指教:
Is there a way without regex or string replaces and so forth to convert a simple date such as:
有没有办法不用正则表达式或字符串替换等来转换一个简单的日期,例如:
Fri Jun 21 00:00:00 UTC+0100 2013
To a ISO8601
formatted date yy-mm-dd
?
到 ISO8601
格式的日期 yy-mm-dd
?
PS: 8601
date only, not date time.
PS:8601
仅日期,而不是日期时间。
采纳答案by nnnnnn
Without regexes or string replaces? Yes, assuming that the format is fixed you could use .slice()
and/or .substr()
to extract the particular bits you need and rearrange them (unless such methods fall into your category of "and so forth"):
没有正则表达式或字符串替换?是的,假设格式是固定的,您可以使用.slice()
和/或.substr()
提取您需要的特定位并重新排列它们(除非这些方法属于您的“等等”类别):
var input = "Fri Jun 21 00:00:00 UTC+0100 2013";
var year = input.slice(-4),
month = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'].indexOf(input.substr(4,3))+1,
day = input.substr(8,2);
var output = year + '-' + (month<10?'0':'') + month + '-' + day;
Or you couldgo ahead and get silly with a regex replace:
或者您可以继续使用正则表达式替换变得愚蠢:
var output = input.replace(/^[^\s]+\s([^\s]+)\s(\d+)\s.*(\d{4})$/,function(m,p1,p2,p3) {
var month = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'].indexOf(p1)+1;
return p3 + '-' + (month<10?'0':'') + month + '-' + (p2.length===1?'0':'') + p2;
});
Of course you'd probably want to wrap such code in a reformatDate()
method.
当然,您可能希望将此类代码包装在一个reformatDate()
方法中。
(For a "simple" reformatting of a date string,the Date
object and its methods aren't particularly helpful unless the starting format is one recognised by Date.parse()
.)
(对于“简单的”重新格式化的日期的串,该Date
对象及其方法,没有特别有用除非起始格式由识别一个Date.parse()
。)
回答by fmsf
Use moment.js http://momentjs.com/
使用moment.js http://momentjs.com/
moment(new Date(), "YYYY-MM-DD HH:mm Z");
or:
或者:
var date = moment("Fri Jun 21 00:00:00 UTC+0100 2013");
moment("Fri Jun 21 00:00:00 UTC+0100 2013", "YYYY-MM-DD HH:mm Z");
You can parse it and format it very easily whatever way you want http://momentjs.com/docs/it is compatible with ISO-8601 dates for parsing as well.
你可以很容易地解析它并以任何你想要的方式格式化它http://momentjs.com/docs/它也兼容 ISO-8601 日期进行解析。
回答by u231664
Yes !
是的 !
the date function in javascript.
javascript中的日期函数。
var d = new Date("Fri Jun 21 00:00:00 UTC+0100 2013")
alert( d.getFullYear() + '-' + d.getUTCMonth() + '-' + d.getUTCDay())
2 lines of code :)
2行代码:)
more info here : http://www.w3schools.com/jsref/jsref_obj_date.asp
回答by Panagiotis
Why dont you try to use the get
functions, like getDate()
, getMonth()
, etc. For example:
你为什么不尝试使用get
功能,如getDate()
,getMonth()
等。例如:
var today = new Date();
var d1 = new Date();
alert(d1);
var date = d1.getDate();
var month = d1.getMonth() + 1;
var year = d1.getFullYear();
Then configure the string the way you want it to appear...!
然后按照您希望它出现的方式配置字符串...!