最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - Cleaner way to convert dd-mm-yyyy to mm-dd-yyyy format in Javascript - Stack Overflow

programmeradmin10浏览0评论

I have this date as string with me 15-07-2011 which is in the format dd-mm-yyyy. I needed to create a Date object from this string. So I have to convert the date in dd-mm-yyyy to mm-dd-yyyy format.

What I did is the following.

var myDate = '15-07-2011';
var chunks = myDate.split('-');

var formattedDate = chunks[1]+'-'+chunks[0]+'-'+chunks[2];

Now I got the string 07-15-2011 which is in mm-dd-yyyy format, and I can pass it to the Date() constructor to create a Date object. I wish to know if there is any cleaner way to do this.

I have this date as string with me 15-07-2011 which is in the format dd-mm-yyyy. I needed to create a Date object from this string. So I have to convert the date in dd-mm-yyyy to mm-dd-yyyy format.

What I did is the following.

var myDate = '15-07-2011';
var chunks = myDate.split('-');

var formattedDate = chunks[1]+'-'+chunks[0]+'-'+chunks[2];

Now I got the string 07-15-2011 which is in mm-dd-yyyy format, and I can pass it to the Date() constructor to create a Date object. I wish to know if there is any cleaner way to do this.

Share Improve this question asked Jul 13, 2011 at 18:45 SparkySparky 4,8798 gold badges41 silver badges52 bronze badges 4
  • @Lime No, I meant cleaner - widely accepted practice. – Sparky Commented Jul 13, 2011 at 19:08
  • 1 Firefox 3.6 does noes not understand "mm-dd-yyyy", use either "mm/dd/yyyy" or "yyyy-mm-dd". I would favor the later, as it's an ISO standard. – Edgar Bonet Commented Jul 13, 2011 at 19:11
  • @Sparky Just clarifying, I thought it looked pretty clean as is. This could be cleaner if your in to regexs '15-07-2011'.replace(/(\d*)-(\d*)-(\d*)/,'$2-$1-$3') – Lime Commented Jul 13, 2011 at 19:14
  • @Edgar Thanks for pointing out that "mm-dd-yyyy" is not supported in Firefox 3.6. I ended up having the problem in Firefox 5 and was totally confused as the code was working fine in Chrome. Your note helped a lot. Thanks. – Sparky Commented Jul 14, 2011 at 4:52
Add a ment  | 

9 Answers 9

Reset to default 5

That looks very clean as it is.

Re-arranging chunks of a string is a perfectly "clean" and legitimate way to change a date format.

However, if you're not happy with that (maybe you want to know that the string you're re-arranging is actually a valid date?), then I remend you look at DateJS, which is a full-featured date handling library for Javascript.

Depends what you mean by cleaner

var myDate = '15-07-2011';
var chunks = myDate.split('-');
var formattedDate = [chunks[1],chunks[0],chunks[2]].join("-");

Some would say this is cleaner, but it does essentially the same.

var formattedDate = chunks[1] + '-' + chunks[0] + '-' + chunks.pop();
var c = '01-01-2011'.split('-');
var d = new Date(c[2],c[1]-1,c[0]);

If you wanted to you could cut down on some variables.

var date = '15-07-2011'.split('-');
date = date[1]+'-'+date[0]+'-'+date[2];

If you want a one liner

var date = '15-07-2011'.replace(/(\d*)-(\d*)-(\d*)/,'$2-$1-$3')

I'll add my opinion that your solution is perfectly valid, but if you want something different:

var myDate = '15-07-2011';
myDate.split('-').reverse().join('-');

Will give you '2011-07-15' which, although not exactly what you asked for, will be parsed properly by Date

I wrote a library for parsing, manipulating, and formatting strings named Moment.js

var date = moment('15-07-2011', 'DD-MM-YYYY').format('DD-MM-YYYY');

Try

myDate.format("mm-dd-yyyy");
发布评论

评论列表(0)

  1. 暂无评论