在 Javascript 中将 dd-mm-yyyy 转换为 mm-dd-yyyy 格式的更简洁的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6683954/
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
Cleaner way to convert dd-mm-yyyy to mm-dd-yyyy format in Javascript
提问by Sparky
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.
我把这个日期作为字符串 15-07-2011,格式为dd-mm-yyyy
. 我需要Date
从这个字符串创建一个对象。所以我必须将日期转换dd-mm-yyyy
为mm-dd-yyyy
格式。
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.
现在我得到了格式为 07-15-2011 的字符串mm-dd-yyyy
,我可以将它传递给Date()
构造函数来创建一个Date
对象。我想知道是否有任何更清洁的方法来做到这一点。
采纳答案by Paul Sonier
That looks very clean as it is.
看起来很干净。
回答by Spudley
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 recommend you look at DateJS, which is a full-featured date handling library for Javascript.
但是,如果您对此不满意(也许您想知道您重新排列的字符串实际上是一个有效日期?),那么我建议您查看DateJS,它是一个功能齐全的日期处理库对于 Javascript。
回答by Sean Kinsey
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.
有人会说这更干净,但它的作用基本相同。
回答by Jose Faeti
var formattedDate = chunks[1] + '-' + chunks[0] + '-' + chunks.pop();
回答by Lime
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*)/,'--')
回答by Satyajit
var c = '01-01-2011'.split('-');
var d = new Date(c[2],c[1]-1,c[0]);
回答by Mark Kahn
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
会给你“2011-07-15”,虽然不是你所要求的,但会被正确解析 Date
回答by timrwood
I wrote a library for parsing, manipulating, and formatting strings named Moment.js
我编写了一个名为Moment.js 的用于解析、操作和格式化字符串的库
var date = moment('15-07-2011', 'DD-MM-YYYY').format('DD-MM-YYYY');
回答by boca
Try
尝试
myDate.format("mm-dd-yyyy");