Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format JavaScript date to mm/dd/yyyy?

Tags:

javascript

Im getting the date like 'Wed Nov 08 2017 00:00:00 GMT-0800 (Pacific Standard Time)' and putting the value in an input.

Is there a way to parse the datetime into a date, add five days to it, and then format it like mm/dd/yyyy?

I made a https://jsfiddle.net/ax6qqw0t/

startingDate = new Date(5/5/2017);
var adjustedDate = addDays(startingDate,13);
$('#max_products').val(adjustedDate);
like image 706
anatp_123 Avatar asked Apr 16 '26 17:04

anatp_123


1 Answers

Here are useful one-liners to get date string in various formats:

US-Date formatted as m/d/yyyy and mm/DD/yyyy (padded zeroes)

>new Date().toLocaleDateString(); //if your locale is US, otherwise toLocaleDateString('en-US');
"2/7/2020"
>new Date().toLocaleDateString('es-pa'); //zero-padded US date
"02/07/2020"

Date formatted as yyyy-mm-dd, and yyyymmdd: useful for sorting

>new Date().toLocaleDateString('en-ca');
"2020-02-07"
>new Date().toLocaleDateString('en-ca').replace(/-/g,'');
"20200207"

This list covers most other international formats:

d-M-yyyy        nl
dd.MM.yyyy      en-CH
d. M. yyyy      sk
d.MM.yyyy       pl
d.M.yyyy        de
dd/MM/yyyy      en-DE
d/MM/yyyy       en-NZ
d/M/yyyy        ca
d/M/yyyy        mr (devanagari numbers)
yyyy-M-d        bs
yyyy. MM. dd.   hu
yyyy. M. d.     ko-KP
yyyy/MM/dd      en-ZA
yyyy/MM/dd      ar-EG (arabic numbers)
yyyy/M/d        zh-CN

Finally, the ECMAScript Intl.DateTimeFormat is the Internationalization API that gives you lot of formatting flexibility. Here is a link to the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

like image 179
Vijay Jagdale Avatar answered Apr 19 '26 09:04

Vijay Jagdale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!