如何将Date对象格式化为字符串?
当前回答
大家好,我有一个代码,用于为日期添加日期和格式。
const FechaMaxima = new Date();
FechaMaxima.setDate(FechaMaxima.getDate() + 6);
FechaMaxima.toISOString().substring(0, 10);
其他回答
Use:
thisDate = new Date(parseInt(jsonDateString.replace('/Date(', '')));
formattedDate = (thisDate.getMonth() + 1) + "/" + (thisDate.getDate()+1) + "/" + thisDate.getFullYear();
这采用JSON日期“/date(1429573751663)/”,并生成格式化字符串:
"4/21/2015"
字符串转换
// date
const dateConvert = {
dasher: dt => {
let m = (dt.getMonth() + 1) === 13 ? 1 : (dt.getMonth() + 1);
m = m < 10 ? `0${m}` : m.toString();
let d = dt.getDate();
d = d < 10 ? `0${d}` : d.toString();
return `${dt.getFullYear()}-${m}-${d}`;
},
slasher: dt => {
return dateConvert.slash(dateConvert.dasher(dt));
},
dash: str => {
// 03/11/2022 -> 2022-03-11
let [d, m, y] = str.split('/');
return `${y}-${m}-${d}`;
},
slash: str => {
// 2022-03-11 -> 03/11/2022
let [y, m, d] = str.split('-');
return `${d}/${m}/${y}`
}
}
// console.log(dateConvert.dasher(new Date('01/31/2001')));
好的,我们有一个叫做Intl的东西,这对在JavaScript中格式化日期非常有用:
您的日期如下:
var date = '10/8/2010';
然后使用如下所示的新Date()更改为Date:
date = new Date(date);
现在,您可以使用以下区域设置列表以任何方式对其进行格式化:
date = new Intl.DateTimeFormat('en-AU').format(date); // Australian date format: "8/10/2010"
date = new Intl.DateTimeFormat('en-US').format(date); // USA date format: "10/8/2010"
date = new Intl.DateTimeFormat('ar-EG').format(date); // Arabic date format: "٨/١٠/٢٠١٠"
如果您确实想要上面提到的格式,可以执行以下操作:
date = new Date(Date.UTC(2010, 7, 10, 0, 0, 0));
var options = {year: "numeric", month: "short", day: "numeric"};
date = new Intl.DateTimeFormat("en-AU", options).format(date).replace(/\s/g, '-');
结果将是:
"10-Aug-2010"
有关详细信息,请参阅Intl API和Intl.DateTimeFormat文档。
这可以帮助:
export const formatDateToString = date => {
if (!date) {
return date;
}
try {
return format(parse(date, 'yyyy-MM-dd', new Date()), 'dd/MM/yyyy');
} catch (error) {
return 'invalid date';
}
};
以下代码将允许您将日期格式设置为DD-MM-YYYY(2017年12月27日)或DD-MM-YYYY(2017年10月26日):
/** Pad number to fit into nearest power of 10 */
function padNumber(number, prependChar, count) {
var out = '' + number; var i;
if (number < Math.pow(10, count))
while (out.length < ('' + Math.pow(10, count)).length) out = prependChar + out;
return out;
}
/* Format the date to 'DD-MM-YYYY' or 'DD MMM YYYY' */
function dateToDMY(date, useNumbersOnly) {
var months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec'
];
return '' + padNumber(date.getDate(), '0', 1) +
(useNumbersOnly? '-' + padNumber(date.getMonth() + 1, '0', 1) + '-' : ' ' + months[date.getMonth()] + ' ')
+ date.getFullYear();
}
更改date.getFullYear()和padNumber(date.getDate(),“0”,1)的顺序,以生成dateToYMD()函数。
有关详细信息,请参见repl.it示例。
推荐文章
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA