简体   繁体   English

如何将 /Date(1606867200000)/ 这样的日期转换为 dd/MM/yyyy 格式的字符串

[英]How do I convert a date like this /Date(1606867200000)/ to a string in format dd/MM/yyyy

I'm using Bootstraptable and want to convert a date column to a British formatted date string.我正在使用 Bootstraptable 并希望将日期列转换为英国格式的日期字符串。 If I do not change the value the table displays dates as /Date(1606867200000)/如果我不更改值,表格将日期显示为 /Date(1606867200000)/

I've set the column with a data-formatter = "dateFormatter"我用 data-formatter = "dateFormatter" 设置了列

My formatting function is我的格式 function 是

function dateFormatter(value) {
    if (value == "" || value == null) {
        return "-";
    } else {
        console.log(value);
        return value.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' });
    }
}

I've tried new Date(value) this gives invalid date.我试过 new Date(value) 这给出了无效的日期。

Any help appreciated.任何帮助表示赞赏。

Your date is I guess stored in Mongodb or some other kind of DB so please remove that formting and then convert to date like我猜您的日期存储在 Mongodb 或其他类型的数据库中,因此请删除该格式,然后转换为日期,例如

const time = + "/Date(1606867200000)/".replace(/\D/g, "");
console.log( new Date( time ));

I guess, problem is related to the format of input.我想,问题与输入格式有关。 You need to convert it do Date explicitly.您需要明确地将其转换为Date

Try it:试试看:

const d = new Date(value); 
console.log(d.toLocaleString('en-GB', { timeZone: 'UTC' }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM