简体   繁体   中英

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. If I do not change the value the table displays dates as /Date(1606867200000)/

I've set the column with a data-formatter = "dateFormatter"

My formatting function is

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.

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

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.

Try it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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