Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Date in Consistent Format

I am having a function which return the date based on some condition (last 30 day).

const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
const startTime = start.toLocaleString().split(",")[0];
console.log(startTime)

In Mac OS,I am getting the output in dd/mm/yyyy format (which is the desired format).

But in Windows , I am getting the output in mm/dd/yyyy. How can I make it consistent irespective of platform. Desired format is dd/mm/yyy

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use toLocaleDateString with ‘en-GB’

const start = new Date();
start.setDate(start.getDate() - 30)
const startTime = start.toLocaleDateString('en-GB');
console.log(startTime)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading