Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to find a specific format to convert the date to but I can't figure out what is it using.
I want it to display the date as the following example:
2021-08-23T15:21:37.513


What I have tried:

I have tried the following:
new Date(Date.UTC(this.minDate.getFullYear(), this.minDate.getMonth(), this.minDate.getDate())).toLocaleString()

Also I have tried toIsoString() and it is giving me similar but with a Z at the end.
I need to compare the date with another of the same format later on too.

Can someone tell me how to get it please?
Thank you
Posted
Updated 21-Sep-21 0:36am
v2

See here: How to format a JavaScript date? - Stack Overflow[^]

But ...
Quote:
I need to compare the date with another of the same format later on too.

In that case, don't convert them. You need to compare them as dates, not strings - so if you convert them to strings first you'll only have to convert them back to dates before you can compare them!
 
Share this answer
 
Just use some library designed for datetime manipulation. For example day.js

With it, you can compare your dates and then format them like this
dayjs(yourDate).format('YYYY-MM-DDTHH:mm:ss') 


Docs on formatting dates in day.js
 
Share this answer
 
I had to create a function:

convToTime(date: Date) {
            if (date !== null) 
            {
                this.tDate = new Date(date.toString().substring(0,10));
                return this.tDate.getTime();
            }
        }

and called it this way:

${this.convToTime(row.CreateDate)}

All working properly now and as needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900