用JavaScript将日期从一种格式转换为另一种格式

Convert Date from one format to another format in JavaScript

本文关键字:格式 一种 转换 另一种 JavaScript 日期      更新时间:2023-09-26

我有一个javascript格式的日期字符串,格式为#1。我需要将其转换为#2格式。

当一种格式为"dd/mm/yy",而另一种格式则为"mm/dd/y"时,问题就开始了。

格式是动态变化的,我有字符串格式,但我需要一个类似的函数

   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;
}

您可以使用下面的函数

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;
 }

您似乎应该使用query dateformat git hub库来实现jquery dateformat
或者你可以使用像日期-时间格式这样的普通函数,使用库来配置格式模板