admin管理员组文章数量:1201801
I have this date fromat:
Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)
How can I reformat it to something more friendlier such as 2/2/2015
I am using javascript.
I tried using .format
and dateFormat
but they both return undefined value.
How can I do this? I don't want to use any regex please.
I have this date fromat:
Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)
How can I reformat it to something more friendlier such as 2/2/2015
I am using javascript.
I tried using .format
and dateFormat
but they both return undefined value.
How can I do this? I don't want to use any regex please.
Share Improve this question asked Feb 2, 2015 at 6:00 Max PainMax Pain 1,2377 gold badges19 silver badges34 bronze badges 6- Refer this. stackoverflow.com/questions/12409299/… – Saravana Kumar Commented Feb 2, 2015 at 6:03
- I already have the date, I just want to reformat it. – Max Pain Commented Feb 2, 2015 at 6:04
- Refer this stackoverflow.com/questions/7245176/reformatting-date-string – Arunprasanth K V Commented Feb 2, 2015 at 6:06
- use this. var date = new Date('Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)') to convert string to date – Saravana Kumar Commented Feb 2, 2015 at 6:07
- I've answered here: stackoverflow.com/questions/28038634/… – rony36 Commented Feb 2, 2015 at 6:10
5 Answers
Reset to default 8If you want the simplest way just concat all of the separate parts
var output = dt.getMonth( ) + 1 +'/'+ dt.getDate( ) + '/' +dt.getFullYear( );
There are a few libraries that will handle more advanced stuff if you need it, but that is the lightest way I can think of doing what you are asking.
Use this awesome library. Moment JS
For your case it would be some thing link
var dateString = 'Mon Feb 02 2015 05:18:44 GMT+0000';
var date = new Moment(dateString);
alert(date.format('MM/dd/YYYY'));
var d = new Date();
var n = d.toLocaleDateString();
I think this will give you desired output.
You can pass it to Date
Object:
var dateString = "Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)";
var date = new Date(dateString);
date.getDate(); // > 2 (the day number)
date.getMonth(); // > 1 (the month number as 0 is January, 11 is December)
You can also find a lib to do the format job or format yourself.
There are many ways to parse and re-format a date. Here is a very simple method that works for your date format:
var d = 'Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)';
var dStr = new Date(d).toDateString();
The value of dSTr
will be something like 'Sun Feb 01 2015'.
本文标签: Javascript reformat date stringStack Overflow
版权声明:本文标题:Javascript reformat date string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738555783a2098256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论