Return 来自 Date() 对象的 dd-mm-yyyy
Return dd-mm-yyyy from Date() object
所需的 return 值应该是格式为 dd-mm-yyyy
.
的字符串
我正在尝试将格式日期 dd-mm-yyyy 提供给 ISOString 并添加 GMT,但代码为我提供了这种格式。我该怎么办?
new Date().toISOString()
.replace(/T/, ' '). // replace T with a space
.replace(/\..+/, ''); // delete the dot and everything after
'2012-11-04 14:55:45'
im looking for 04-11-2012 date format
使用今天的日期(作为 ISO 字符串,当前为“2016-03-08T13:51:13.382Z”),您可以这样做:
new Date().toISOString().replace(/T.*/,'').split('-').reverse().join('-')
这个输出是:
-> "08-03-2016"
这个:
- 抓取日期。
- 将其转换为 ISO 字符串。
- 替换 'T' 及其后的所有内容。
- 通过拆分任何连字符 ('-') 将其转换为数组。 (
["2016", "03", "08"]
)
- 反转数组的顺序。 (
["08", "03", "2016"]
)
- 将数组作为字符串重新加入,用连字符分隔每个值。
这是一个使用您的日期 (2012-11-04T14:55:45.000Z) 作为输入的演示:
var input = "2012-11-04T14:55:45.000Z",
output;
output = new Date(input).toISOString().replace(/T.*/,'').split('-').reverse().join('-');
document.getElementById('input').innerHTML = input;
document.getElementById('output').innerHTML = output;
<p><strong>Input:</strong> <span id=input></span></p>
<p><strong>Output:</strong> <span id=output></span></p>
您可以使用 new Date().toLocaleDateString("en-US");
到 return 只有日期。今天 return 秒 "3/8/2016"
。
new Date().toLocaleDateString().replace(/\//g, '-');
会将其更改为带破折号的输出。今天 return "3-8-2016"
。
对于您的示例“2012-11-04 14:55:45”
您可以在一行中执行:new Date('2012-11-04 14:55:45').toISOString().split('T')[0]
:)
您可以通过添加时区偏移量将本地日期转换为 UTC 日期,然后调用 toLocaleDateString
(英国格式),同时将斜线替换为破折号并删除逗号。
// Adapted from:
const toLocaleUTCDateString = (date, locales, options) =>
new Date(date.valueOf() + (date.getTimezoneOffset() * 6e4))
.toLocaleDateString(locales, options);
// 'en-GB' === 'dd/mm/yyyy'
const formatDate = date =>
toLocaleUTCDateString(date, 'en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
.replace(/\//g, '-').replace(/,/, '');
const date = new Date();
console.log({
'ISO-8601': date.toISOString(),
'Custom': formatDate(date)
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
或者,您可以尝试解析 ISO 8601 字符串:
const formatDate = _date =>
(([year, month, date, hour, minute, second, milliseconds]) =>
`${date}-${month}-${year} ${hour}:${minute}:${second}`)
(_date.toISOString().split(/[^\d]/g));
const date = new Date();
console.log({
'ISO-8601': date.toISOString(),
'Custom': formatDate(date)
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
所需的 return 值应该是格式为 dd-mm-yyyy
.
我正在尝试将格式日期 dd-mm-yyyy 提供给 ISOString 并添加 GMT,但代码为我提供了这种格式。我该怎么办?
new Date().toISOString()
.replace(/T/, ' '). // replace T with a space
.replace(/\..+/, ''); // delete the dot and everything after
'2012-11-04 14:55:45'
im looking for 04-11-2012 date format
使用今天的日期(作为 ISO 字符串,当前为“2016-03-08T13:51:13.382Z”),您可以这样做:
new Date().toISOString().replace(/T.*/,'').split('-').reverse().join('-')
这个输出是:
-> "08-03-2016"
这个:
- 抓取日期。
- 将其转换为 ISO 字符串。
- 替换 'T' 及其后的所有内容。
- 通过拆分任何连字符 ('-') 将其转换为数组。 (
["2016", "03", "08"]
) - 反转数组的顺序。 (
["08", "03", "2016"]
) - 将数组作为字符串重新加入,用连字符分隔每个值。
这是一个使用您的日期 (2012-11-04T14:55:45.000Z) 作为输入的演示:
var input = "2012-11-04T14:55:45.000Z",
output;
output = new Date(input).toISOString().replace(/T.*/,'').split('-').reverse().join('-');
document.getElementById('input').innerHTML = input;
document.getElementById('output').innerHTML = output;
<p><strong>Input:</strong> <span id=input></span></p>
<p><strong>Output:</strong> <span id=output></span></p>
您可以使用 new Date().toLocaleDateString("en-US");
到 return 只有日期。今天 return 秒 "3/8/2016"
。
new Date().toLocaleDateString().replace(/\//g, '-');
会将其更改为带破折号的输出。今天 return "3-8-2016"
。
对于您的示例“2012-11-04 14:55:45”
您可以在一行中执行:new Date('2012-11-04 14:55:45').toISOString().split('T')[0]
:)
您可以通过添加时区偏移量将本地日期转换为 UTC 日期,然后调用 toLocaleDateString
(英国格式),同时将斜线替换为破折号并删除逗号。
// Adapted from:
const toLocaleUTCDateString = (date, locales, options) =>
new Date(date.valueOf() + (date.getTimezoneOffset() * 6e4))
.toLocaleDateString(locales, options);
// 'en-GB' === 'dd/mm/yyyy'
const formatDate = date =>
toLocaleUTCDateString(date, 'en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
.replace(/\//g, '-').replace(/,/, '');
const date = new Date();
console.log({
'ISO-8601': date.toISOString(),
'Custom': formatDate(date)
});
.as-console-wrapper { top: 0; max-height: 100% !important; }
或者,您可以尝试解析 ISO 8601 字符串:
const formatDate = _date =>
(([year, month, date, hour, minute, second, milliseconds]) =>
`${date}-${month}-${year} ${hour}:${minute}:${second}`)
(_date.toISOString().split(/[^\d]/g));
const date = new Date();
console.log({
'ISO-8601': date.toISOString(),
'Custom': formatDate(date)
});
.as-console-wrapper { top: 0; max-height: 100% !important; }