在javascript中将dd/mm/yyyy转换为mm/dd/yyyy

sho*_*oab 16 javascript

我想在javascript中将dd/mm/yyyy转换为mm/dd/yyyy.

Koo*_*Inc 17

var initial = 'dd/mm/yyyy'.split(/\//);
console.log( [ initial[1], initial[0], initial[2] ].join('/')); //=> 'mm/dd/yyyy'
// or in this case you could use
var initial = 'dd/mm/yyyy'.split(/\//).reverse().join('/');
Run Code Online (Sandbox Code Playgroud)


Sur*_*ams 9

var date = "24/09/1977";
var datearray = date.split("/");

var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
Run Code Online (Sandbox Code Playgroud)

newdate将包含09/24/1977.split方法会在字符串找到"/"的地方拆分它,所以如果日期字符串是"24/9/1977",它仍然可以工作并返回9/24/1977.


小智 9

这是一个 moment.js 版本。该库可以在http://momentjs.com/找到

//specify the date string and the format it's initially in
var mydate = moment('15/11/2000', 'DD/MM/YYYY'); 

//format that date into a different format
moment(mydate).format("MM/DD/YYYY");

//outputs 11/15/2000
Run Code Online (Sandbox Code Playgroud)


Syl*_*sne 6

转换dd/mon/yyyymm/dd/yyyy

months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05',
'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11',
'dec': '12'};

split = 'dd/mon/yyyy'.split('/');
[months[split[1]], split[0], split[2]].join('/');
Run Code Online (Sandbox Code Playgroud)

转换dd/mm/yyyymm/dd/yyyy

split = 'dd/mm/yyyy'.split('/');
[split[1], split[0], split[2]].join('/');
Run Code Online (Sandbox Code Playgroud)