Javascript 在javascript中将dd/mm/yyyy转换为mm/dd/yyyy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5433313/
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
convert dd/mm/yyyy to mm/dd/yyyy in javascript
提问by shoab
I want to convert dd/mm/yyyy to mm/dd/yyyy in javascript.
我想在javascript中将dd/mm/yyyy转换为mm/dd/yyyy。
回答by KooiInc
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('/');
回答by Surreal Dreams
var date = "24/09/1977";
var datearray = date.split("/");
var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
newdate will contain 09/24/1977. The split method will split the string wherever it finds a "/", so if the date string was "24/9/1977", it would still work and return 9/24/1977.
newdate 将包含 09/24/1977。split 方法将在找到“/”的任何地方拆分字符串,因此如果日期字符串是“24/9/1977”,它仍然可以工作并返回 9/24/1977。
回答by Sylvain Defresne
Convert dd/mon/yyyy
to mm/dd/yyyy
:
转换dd/mon/yyyy
为mm/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('/');
Convert dd/mm/yyyy
to mm/dd/yyyy
:
转换dd/mm/yyyy
为mm/dd/yyyy
:
split = 'dd/mm/yyyy'.split('/');
[split[1], split[0], split[2]].join('/');
回答by will-yama
This is a moment.js version. The library can be found at http://momentjs.com/
这是一个 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
回答by sreenivasulu palavarapu
var months = new Array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
var ddSplit = '12/23/2011'.split('/'); //date is mm/dd/yyyy format
alert(ddSplit[1]+'-'+months[ddSplit[0]-1] + '-' + ddSplit[2])
回答by leoinfo
Try this example where a regular expression is used to find and switch the parts of the string:
试试这个例子,其中使用正则表达式来查找和切换字符串的各个部分:
var d1 = "03/25/2011";
var d2 = d1.replace(/^(\d{1,2}\/)(\d{1,2}\/)(\d{4})$/,"");
alert("d1:"+d1 +"\n d2:"+d2 )
/*
// OUTPUT
d1: 03/25/2011
d2: 25/03/2011
*/
EDIT:
编辑:
I noticed that no other answer is using regular expression and I really wonder why... :)
我注意到没有其他答案使用正则表达式,我真的很想知道为什么...... :)
回答by Johan Hoeksma
var date = "31/03/2017 14:28";
var regex = /(\d+)/g;
var d = date.match(regex);
var out = d[2] + "-" + d[1] + "-" + d[0] + " " + d[3] + ":" + d[4];
var dag = new Date(out);
console.log(dag);
This is only solution for numbers with this regex
这只是具有此正则表达式的数字的解决方案
回答by Luis Hernández
You can use the following function specified, 1 date and 2 the date separator (optional) The date will be returned in the desired format.
您可以使用以下指定的函数,1 个日期和 2 个日期分隔符(可选) 日期将以所需格式返回。
const setFormatDDMMYYYYtoMMDDYYYY = (date, separator = '/') => {
const [day, month, year] = date.split('/');
return month + separator + day + separator + year;
};
回答by Mk Sz
Try this, it works for me:
试试这个,它对我有用:
var fechaLatinFormat= "05-10-2016";//octuber 5, 2016 - octubre 5 del 2016
var datePieces = fechaLatinFormat.split("-");
var preFinalDate = new Date(parseInt(datePieces[2]), parseInt(datePieces[1]) - 1, parseInt(datePieces[0]));
console.log(preFinalDate.format("mm-dd-yyyy"));
Or:
或者:
var fechaLatinFormat= "05-10-2016";//octuber 5, 2016 - octubre 5 del 2016
var datePieces = fechaLatinFormat.split("-");
var preFinalDate = [datePieces[1] , datePieces[0] , datePieces[2]];
console.log(preFinalDate.join("-"));
回答by arun
use this function
使用这个功能
function dateFormat(existingFormat,neededFormat,existingSeperator,newSeperator,dateValue)
{
var exst = existingFormat.toLowerCase().split(existingSeperator);
var d = dateValue.split(existingSeperator);
d[exst[0]] = d[0]; d[exst[1]]=d[1]; d[exst[2]]=d[2];
var newst = neededFormat.toLowerCase().split(existingSeperator);
newDate = d[newst[0]] +newSeperator+d[newst[1]] +newSeperator+d[newst[2]];
return(newDate);
}
var dd = dateFormat('dd/yy/mm','mm/dd/yy','/','-','30/87/05');//dateFormat(existingFormat,neededFormat,existingSeperator,newSeperator,dateValue)
//use same name for date month and year in both formats
//对两种格式的日期月份和年份使用相同的名称
You can use this function for converting any format date(including month date year only) to any format. :)
您可以使用此功能将任何格式的日期(仅包括月日期年)转换为任何格式。:)