[英]Convert Date from one format to another format in JavaScript
我在javascript中有一個日期字符串,格式為#1。 我需要將其轉換為格式 #2。
當一種格式是“dd/mm/yy”而另一種格式是“mm/dd/yy”時,問題就開始了。
格式動態變化,我將格式作為字符串,但我需要一個像
Date newDate = convert(currentDate, currentFormatString, newFormatString).
我該怎么做?
您應該查看momentjs ,它是一個 javascript 日期/時間庫。 有了它,您可以輕松地在不同格式的日期之間進行轉換。 在您的情況下,它將是:
string newDate = moment(currentDate, currentFormatString).format(newFormatString)
例如, moment("21/10/14", "DD/MM/YY").format("MM/DD/YY")
將返回"10/21/14"
您可以使用 momnet.js 的上述答案或以下功能進行拆分和使用
對於使用 moment.js,請確保以大寫形式輸入舊格式和新格式
function parseDate(strDate) {
var dateFormat = $('#currentDateFormat').val();
var str;
var res;
var strFormat = dateFormat.split('.');
if (strFormat.length != 3)
strFormat = dateFormat.split('/');
if (strFormat.length != 3)
strFormat = dateFormat.split('-');
str = strDate.split('.');
if (str.length != 3)
str = strDate.split('/');
if (str.length != 3)
str = strDate.split('-');
if (strFormat[0].substr(0, 1) == 'd' && strFormat[1].substr(0, 1) == 'M' &&
strFormat[2].substr(0, 1) == 'y') // for dd MM yyyy
res = new Date(str[2], str[1], str[0]);
if (strFormat[0].substr(0, 1) == 'M' && strFormat[1].substr(0, 1) == 'd' &&
strFormat[2].substr(0, 1) == 'y') // for MM dd yyyy
res = new Date(str[2], str[0], str[1]);
if (strFormat[0].substr(0, 1) == 'y' && strFormat[1].substr(0, 1) == 'M' &&
strFormat[2].substr(0, 1) == 'd')
res = new Date(str[0], str[1], str[2]);
if (strFormat[0].substr(0, 1) == 'y' && strFormat[1].substr(0, 1) == 'd' &&
strFormat[2].substr(0, 1) == 'M')
res = new Date(str[0], str[2], str[1]);
return res;
}
看來你應該使用 query-dateformat git hub的庫來處理 jquery-dateformat
或者你可以使用像date-time-formate這樣的普通函數,使用庫來配置格式模板
您可以使用下面的功能
console.log(changeDateFormat('12/1/2020','dd/MM/yyyy','MM/dd/yyyy'));
function changeDateFormat(value, inputFormat, outputFormat) {
let outputSplitter = "/";
let strOutputFormat = outputFormat.split(outputSplitter).map(i => i.toUpperCase());
if (strOutputFormat.length != 3) {
strOutputFormat = outputFormat.split('-');
outputSplitter = '-';
}
if (strOutputFormat.length != 3) throw new Error('wrong output format splitter :(');
let date = null;
if (value instanceof Date) {
date = {
["YYYY"]: value.getUTCFullYear(),
["MM"]: value.getMonth() + 1,
["DD"]: value.getDate()
}
}
if (typeof value == 'string') {
let inputSplitter = "/";
var strInputFormat = inputFormat.split(inputSplitter).map(i => i.toUpperCase());
if (strInputFormat.length != 3) {
strInputFormat = inputFormat.split('-');
inputSplitter = '-';
}
if (strInputFormat.length != 3) throw new Error('wrong input format splitter :(');
let dateElements = value.split(inputSplitter);
if (dateElements.length != 3) throw new Error('wrong value :(');
date = {
[strInputFormat[0]]: dateElements[0],
[strInputFormat[1]]: dateElements[1],
[strInputFormat[2]]: dateElements[2],
}
}
if (!date) throw new Error('unsupported value type:(');
let result = date[strOutputFormat[0]] + outputSplitter
+ date[strOutputFormat[1]] + outputSplitter
+ date[strOutputFormat[2]];
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.