Home > Enterprise >  How to set a date string to a the following format
How to set a date string to a the following format

Time:12-08

I'm using javascript and trying to convert a string to a different format. I did some research on this format but no one has asked about it yet so I thought I may ask. So I have seen that others want to convert a date string that has a shorter length. But this format is different. The format I have now by doing this:

    const now = new Date();
    const currentDate = now.toISOString();

I get this number:

2021-12-05T07:52:47.485Z

However, I want to make it the format like this:

2021-12-05T00:00:00.000 00:00

Is there any way to do so? I don't see others asking about this so not sure if possible

CodePudding user response:

Use moment library:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>    

Like this:

var now = new Date();
now.setHours(0,0,0,0);
var format = 'YYYY-MM-DD[T]HH:mm:ss.SSS[ 00:00]';
console.log(moment(now).format(format));

Link: https://codepen.io/sdssz1365/pen/rNGORKR

  • Related