将日期格式转换为另一种格式

时间:2019-05-16 00:37:13

标签: javascript arrays date format

我已经编写了一个将格式转换的函数(例如,4月16日转换为16.04。),它可以完成工作,但是不幸的是,它不能转换小于10的天。(4月5日,不能转换为05.05)知道为什么吗?谢谢。

var replaceArry = [
[/January 1, /gi,    '01.01.'],
[/January 2, /gi,    '02.01.'],
[/January 3, /gi,    '03.01.'],
[/January 4, /gi,    '04.01.'],
[/January 5, /gi,    '05.01.'],
[/January 6, /gi,    '06.01.'],
[/January 7, /gi,    '07.01.'],
[/January 8, /gi,    '08.01.'],
[/January 9, /gi,    '09.01.'],
[/January 10, /gi,    '10.01.'],
[/January 11, /gi,    '11.01.'],
[/January 12, /gi,    '12.01.'],
[/January 13, /gi,    '13.01.'],
[/January 14, /gi,    '14.01.'],
[/January 15, /gi,    '15.01.'],
[/January 16, /gi,    '16.01.'],
[/January 17, /gi,    '17.01.'],
[/January 18, /gi,    '18.01.'],
[/January 19, /gi,    '19.01.'],
[/January 20, /gi,    '20.01.'],
[/January 21, /gi,    '21.01.'],
[/January 22, /gi,    '22.01.'],
[/January 23, /gi,    '23.01.'],
[/January 24, /gi,    '24.01.'],
[/January 25, /gi,    '25.01.'],
[/January 26, /gi,    '26.01.'],
[/January 27, /gi,    '27.01.'],
[/January 28, /gi,    '28.01.'],
[/January 29, /gi,    '29.01.'],
[/January 30, /gi,    '30.01.'],
[/January 31, /gi,    '31.01.'],
[/February 1, /gi,    '01.02.'],
[/February 2, /gi,    '02.02.'],
[/February 3, /gi,    '03.02.'],
[/February 4, /gi,    '04.02.'],
[/February 5, /gi,    '05.02.'],
[/February 6, /gi,    '06.02.'],
[/February 7, /gi,    '07.02.'],
[/February 8, /gi,    '08.02.'],
[/February 9, /gi,    '09.02.'],
[/February 10, /gi,    '10.02.'],
[/February 11, /gi,    '11.02.'],
[/February 12, /gi,    '12.02.'],
[/February 13, /gi,    '13.02.'],
[/February 14, /gi,    '14.02.'],
[/February 15, /gi,    '15.02.'],
[/February 16, /gi,    '16.02.'],
[/February 17, /gi,    '17.02.'],
[/February 18, /gi,    '18.02.'],
[/February 19, /gi,    '19.02.'],
[/February 20, /gi,    '20.02.'],
[/February 21, /gi,    '21.02.'],
[/February 22, /gi,    '22.02.'],
[/February 23, /gi,    '23.02.'],
[/February 24, /gi,    '24.02.'],
[/February 25, /gi,    '25.02.'],
[/February 26, /gi,    '26.02.'],
[/February 27, /gi,    '27.02.'],
[/February 28, /gi,    '28.02.'],
[/February 29, /gi,    '29.02.'],
[/February 30, /gi,    '30.02.'],
[/February 31, /gi,    '31.02.'],
[/March 1, /gi,    '01.03.'],
[/March 2, /gi,    '02.03.'],
[/March 3, /gi,    '03.03.'],
[/March 4, /gi,    '04.03.'],
[/March 5, /gi,    '05.03.'],
[/March 6, /gi,    '06.03.'],
[/March 7, /gi,    '07.03.'],
[/March 8, /gi,    '08.03.'],
[/March 9, /gi,    '09.03.'],
[/March 10, /gi,    '10.03.'],
[/March 11, /gi,    '11.03.'],
[/March 12, /gi,    '12.03.'],
[/March 13, /gi,    '13.03.'],
[/March 14, /gi,    '14.03.'],
[/March 15, /gi,    '15.03.'],
[/March 16, /gi,    '16.03.'],
[/March 17, /gi,    '17.03.'],
[/March 18, /gi,    '18.03.'],
[/March 19, /gi,    '19.03.'],
[/March 20, /gi,    '20.03.'],
[/March 21, /gi,    '21.03.'],
[/March 22, /gi,    '22.03.'],
[/March 23, /gi,    '23.03.'],
[/March 24, /gi,    '24.03.'],
[/March 25, /gi,    '25.03.'],
[/March 26, /gi,    '26.03.'],
[/March 27, /gi,    '27.03.'],
[/March 28, /gi,    '28.03.'],
[/March 29, /gi,    '29.03.'],
[/March 30, /gi,    '30.03.'],
[/March 31, /gi,    '31.03.'],
[/April 1, /gi,    '01.04.'],
[/April 2, /gi,    '02.04.'],
[/April 3, /gi,    '03.04.'],
[/April 4, /gi,    '04.04.'],
[/April 5, /gi,    '05.04.'],
[/April 6, /gi,    '06.04.'],
[/April 7, /gi,    '07.04.'],
[/April 8, /gi,    '08.04.'],
[/April 9, /gi,    '09.04.'],
[/April 10, /gi,    '10.04.'],
[/April 11, /gi,    '11.04.'],
[/April 12, /gi,    '12.04.'],
[/April 13, /gi,    '13.04.'],
[/April 14, /gi,    '14.04.'],
[/April 15, /gi,    '15.04.'],
[/April 16, /gi,    '16.04.'],
[/April 17, /gi,    '17.04.'],
[/April 18, /gi,    '18.04.'],
[/April 19, /gi,    '19.04.'],
[/April 20, /gi,    '20.04.'],
[/April 21, /gi,    '21.04.'],
[/April 22, /gi,    '22.04.'],
[/April 23, /gi,    '23.04.'],
[/April 24, /gi,    '24.04.'],
[/April 25, /gi,    '25.04.'],
[/April 26, /gi,    '26.04.'],
[/April 27, /gi,    '27.04.'],
[/April 28, /gi,    '28.04.'],
[/April 29, /gi,    '29.04.'],
[/April 30, /gi,    '30.04.'],
[/April 31, /gi,    '31.04.'],
[/May 1, /gi,    '01.05.'],
[/May 2, /gi,    '02.05.'],
[/May 3, /gi,    '03.05.'],
[/May 4, /gi,    '04.05.'],
[/May 5, /gi,    '05.05.'],
[/May 6, /gi,    '06.05.'],
[/May 7, /gi,    '07.05.'],
[/May 8, /gi,    '08.05.'],
[/May 9, /gi,    '09.05.'],
[/May 10, /gi,    '10.05.'],
[/May 11, /gi,    '11.05.'],
[/May 12, /gi,    '12.05.'],
[/May 13, /gi,    '13.05.'],
[/May 14, /gi,    '14.05.'],
[/May 15, /gi,    '15.05.'],
[/May 16, /gi,    '16.05.'],
[/May 17, /gi,    '17.05.'],
[/May 18, /gi,    '18.05.'],
[/May 19, /gi,    '19.05.'],
[/May 20, /gi,    '20.05.'],
[/May 21, /gi,    '21.05.'],
[/May 22, /gi,    '22.05.'],
[/May 23, /gi,    '23.05.'],
[/May 24, /gi,    '24.05.'],
[/May 25, /gi,    '25.05.'],
[/May 26, /gi,    '26.05.'],
[/May 27, /gi,    '27.05.'],
[/May 28, /gi,    '28.05.'],
[/May 29, /gi,    '29.05.'],
[/May 30, /gi,    '30.05.'],
[/May 31, /gi,    '31.05.'],
[/June 1, /gi,    '01.06.'],
[/June 2, /gi,    '02.06.'],
[/June 3, /gi,    '03.06.'],
[/June 4, /gi,    '04.06.'],
[/June 5, /gi,    '05.06.'],
[/June 6, /gi,    '06.06.'],
[/June 7, /gi,    '07.06.'],
[/June 8, /gi,    '08.06.'],
[/June 9, /gi,    '09.06.'],
[/June 10, /gi,    '10.06.'],
[/June 11, /gi,    '11.06.'],
[/June 12, /gi,    '12.06.'],
[/June 13, /gi,    '13.06.'],
[/June 14, /gi,    '14.06.'],
[/June 15, /gi,    '15.06.'],
[/June 16, /gi,    '16.06.'],
[/June 17, /gi,    '17.06.'],
[/June 18, /gi,    '18.06.'],
[/June 19, /gi,    '19.06.'],
[/June 20, /gi,    '20.06.'],
[/June 21, /gi,    '21.06.'],
[/June 22, /gi,    '22.06.'],
[/June 23, /gi,    '23.06.'],
[/June 24, /gi,    '24.06.'],
[/June 25, /gi,    '25.06.'],
[/June 26, /gi,    '26.06.'],
[/June 27, /gi,    '27.06.'],
[/June 28, /gi,    '28.06.'],
[/June 29, /gi,    '29.06.'],
[/June 30, /gi,    '30.06.'],
[/June 31, /gi,    '31.06.'],
[/July 1, /gi,    '01.07.'],
[/July 2, /gi,    '02.07.'],
[/July 3, /gi,    '03.07.'],
[/July 4, /gi,    '04.07.'],
[/July 5, /gi,    '05.07.'],
[/July 6, /gi,    '06.07.'],
[/July 7, /gi,    '07.07.'],
[/July 8, /gi,    '08.07.'],
[/July 9, /gi,    '09.07.'],
[/July 10, /gi,    '10.07.'],
[/July 11, /gi,    '11.07.'],
[/July 12, /gi,    '12.07.'],
[/July 13, /gi,    '13.07.'],
[/July 14, /gi,    '14.07.'],
[/July 15, /gi,    '15.07.'],
[/July 16, /gi,    '16.07.'],
[/July 17, /gi,    '17.07.'],
[/July 18, /gi,    '18.07.'],
[/July 19, /gi,    '19.07.'],
[/July 20, /gi,    '20.07.'],
[/July 21, /gi,    '21.07.'],
[/July 22, /gi,    '22.07.'],
[/July 23, /gi,    '23.07.'],
[/July 24, /gi,    '24.07.'],
[/July 25, /gi,    '25.07.'],
[/July 26, /gi,    '26.07.'],
[/July 27, /gi,    '27.07.'],
[/July 28, /gi,    '28.07.'],
[/July 29, /gi,    '29.07.'],
[/July 30, /gi,    '30.07.'],
[/July 31, /gi,    '31.07.'],
[/August 1, /gi,    '01.08.'],
[/August 2, /gi,    '02.08.'],
[/August 3, /gi,    '03.08.'],
[/August 4, /gi,    '04.08.'],
[/August 5, /gi,    '05.08.'],
[/August 6, /gi,    '06.08.'],
[/August 7, /gi,    '07.08.'],
[/August 8, /gi,    '08.08.'],
[/August 9, /gi,    '09.08.'],
[/August 10, /gi,    '10.08.'],
[/August 11, /gi,    '11.08.'],
[/August 12, /gi,    '12.08.'],
[/August 13, /gi,    '13.08.'],
[/August 14, /gi,    '14.08.'],
[/August 15, /gi,    '15.08.'],
[/August 16, /gi,    '16.08.'],
[/August 17, /gi,    '17.08.'],
[/August 18, /gi,    '18.08.'],
[/August 19, /gi,    '19.08.'],
[/August 20, /gi,    '20.08.'],
[/August 21, /gi,    '21.08.'],
[/August 22, /gi,    '22.08.'],
[/August 23, /gi,    '23.08.'],
[/August 24, /gi,    '24.08.'],
[/August 25, /gi,    '25.08.'],
[/August 26, /gi,    '26.08.'],
[/August 27, /gi,    '27.08.'],
[/August 28, /gi,    '28.08.'],
[/August 29, /gi,    '29.08.'],
[/August 30, /gi,    '30.08.'],
[/August 31, /gi,    '31.08.'],
[/September 1, /gi,    '01.09.'],
[/September 2, /gi,    '02.09.'],
[/September 3, /gi,    '03.09.'],
[/September 4, /gi,    '04.09.'],
[/September 5, /gi,    '05.09.'],
[/September 6, /gi,    '06.09.'],
[/September 7, /gi,    '07.09.'],
[/September 8, /gi,    '08.09.'],
[/September 9, /gi,    '09.09.'],
[/September 10, /gi,    '10.09.'],
[/September 11, /gi,    '11.09.'],
[/September 12, /gi,    '12.09.'],
[/September 13, /gi,    '13.09.'],
[/September 14, /gi,    '14.09.'],
[/September 15, /gi,    '15.09.'],
[/September 16, /gi,    '16.09.'],
[/September 17, /gi,    '17.09.'],
[/September 18, /gi,    '18.09.'],
[/September 19, /gi,    '19.09.'],
[/September 20, /gi,    '20.09.'],
[/September 21, /gi,    '21.09.'],
[/September 22, /gi,    '22.09.'],
[/September 23, /gi,    '23.09.'],
[/September 24, /gi,    '24.09.'],
[/September 25, /gi,    '25.09.'],
[/September 26, /gi,    '26.09.'],
[/September 27, /gi,    '27.09.'],
[/September 28, /gi,    '28.09.'],
[/September 29, /gi,    '29.09.'],
[/September 30, /gi,    '30.09.'],
[/September 31, /gi,    '31.09.'],
[/October 1, /gi,    '01.10.'],
[/October 2, /gi,    '02.10.'],
[/October 3, /gi,    '03.10.'],
[/October 4, /gi,    '04.10.'],
[/October 5, /gi,    '05.10.'],
[/October 6, /gi,    '06.10.'],
[/October 7, /gi,    '07.10.'],
[/October 8, /gi,    '08.10.'],
[/October 9, /gi,    '09.10.'],
[/October 10, /gi,    '10.10.'],
[/October 11, /gi,    '11.10.'],
[/October 12, /gi,    '12.10.'],
[/October 13, /gi,    '13.10.'],
[/October 14, /gi,    '14.10.'],
[/October 15, /gi,    '15.10.'],
[/October 16, /gi,    '16.10.'],
[/October 17, /gi,    '17.10.'],
[/October 18, /gi,    '18.10.'],
[/October 19, /gi,    '19.10.'],
[/October 20, /gi,    '20.10.'],
[/October 21, /gi,    '21.10.'],
[/October 22, /gi,    '22.10.'],
[/October 23, /gi,    '23.10.'],
[/October 24, /gi,    '24.10.'],
[/October 25, /gi,    '25.10.'],
[/October 26, /gi,    '26.10.'],
[/October 27, /gi,    '27.10.'],
[/October 28, /gi,    '28.10.'],
[/October 29, /gi,    '29.10.'],
[/October 30, /gi,    '30.10.'],
[/October 31, /gi,    '31.10.'],
[/November 1, /gi,    '01.11.'],
[/November 2, /gi,    '02.11.'],
[/November 3, /gi,    '03.11.'],
[/November 4, /gi,    '04.11.'],
[/November 5, /gi,    '05.11.'],
[/November 6, /gi,    '06.11.'],
[/November 7, /gi,    '07.11.'],
[/November 8, /gi,    '08.11.'],
[/November 9, /gi,    '09.11.'],
[/November 10, /gi,    '10.11.'],
[/November 11, /gi,    '11.11.'],
[/November 12, /gi,    '12.11.'],
[/November 13, /gi,    '13.11.'],
[/November 14, /gi,    '14.11.'],
[/November 15, /gi,    '15.11.'],
[/November 16, /gi,    '16.11.'],
[/November 17, /gi,    '17.11.'],
[/November 18, /gi,    '18.11.'],
[/November 19, /gi,    '19.11.'],
[/November 20, /gi,    '20.11.'],
[/November 21, /gi,    '21.11.'],
[/November 22, /gi,    '22.11.'],
[/November 23, /gi,    '23.11.'],
[/November 24, /gi,    '24.11.'],
[/November 25, /gi,    '25.11.'],
[/November 26, /gi,    '26.11.'],
[/November 27, /gi,    '27.11.'],
[/November 28, /gi,    '28.11.'],
[/November 29, /gi,    '29.11.'],
[/November 30, /gi,    '30.11.'],
[/November 31, /gi,    '31.11.'],
[/December 1, /gi,    '01.12.'],
[/December 2, /gi,    '02.12.'],
[/December 3, /gi,    '03.12.'],
[/December 4, /gi,    '04.12.'],
[/December 5, /gi,    '05.12.'],
[/December 6, /gi,    '06.12.'],
[/December 7, /gi,    '07.12.'],
[/December 8, /gi,    '08.12.'],
[/December 9, /gi,    '09.12.'],
[/December 10, /gi,    '10.12.'],
[/December 11, /gi,    '11.12.'],
[/December 12, /gi,    '12.12.'],
[/December 13, /gi,    '13.12.'],
[/December 14, /gi,    '14.12.'],
[/December 15, /gi,    '15.12.'],
[/December 16, /gi,    '16.12.'],
[/December 17, /gi,    '17.12.'],
[/December 18, /gi,    '18.12.'],
[/December 19, /gi,    '19.12.'],
[/December 20, /gi,    '20.12.'],
[/December 21, /gi,    '21.12.'],
[/December 22, /gi,    '22.12.'],
[/December 23, /gi,    '23.12.'],
[/December 24, /gi,    '24.12.'],
[/December 25, /gi,    '25.12.'],
[/December 26, /gi,    '26.12.'],
[/December 27, /gi,    '27.12.'],
[/December 28, /gi,    '28.12.'],
[/December 29, /gi,    '29.12.'],
[/December 30, /gi,    '30.12.'],
[/December 31, /gi,    '31.12.'],
];
var numTerms    = replaceArry.length;
var txtWalker   = document.createTreeWalker (
    document.body,
    NodeFilter.SHOW_TEXT,
    {   acceptNode: function (node) {
            if (node.nodeValue.trim() )
                return NodeFilter.FILTER_ACCEPT;

            return NodeFilter.FILTER_SKIP;
        }
    },
    false
);
var txtNode     = null;

while (txtNode  = txtWalker.nextNode () ) {
    var oldTxt  = txtNode.nodeValue;

    for (var J  = 0;  J < numTerms;  J++) {
        oldTxt  = oldTxt.replace (replaceArry[J][0], replaceArry[J][1]);
    }
    txtNode.nodeValue = oldTxt;
}

2 个答案:

答案 0 :(得分:0)

您可以删除整个数组并按以下格式设置日期格式:

var date = new Date("April 5");
var m = date.getMonth() < 10 ? "0" + (date.getMonth() + 1) + "." : (date.getMonth() + 1) + ".";
var d = date.getDate() < 10 ? "0" + (date.getDate()) + ".": date.getDate() + ".";
var formatted = d + m;

console.log(formatted)将输出05.04.

答案 1 :(得分:-1)

或者,您可以使用JavaScript的DateString功能,如下所示:

let dateString = "April 5, 2019";
let customFormat = new Date(dateString)
  .toLocaleString('en-GB', { month: "2-digit", day: "2-digit"})
  .substring(0, 5)
  .split('/')
//      .reverse()
  .join('.');
console.log(customFormat);
document.querySelector('body').innerText = customFormat;

注释的reverse()函数可让您根据需要将day.month的顺序更改为month.day