Reputation: 1205
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);
Upvotes: 9
Views: 57124
Reputation: 8533
I'm sure you've solved this by now, but it looked like a fun one so I decided to take a stab at it using Intl.DateTimeFormat
.
// get date for five days later
const dateText = 'Fri Dec 29 2017 00:00:00 GMT-0800 (Pacific Standard Time)';
const date = new Date(dateText);
date.setDate(date.getDate() + 5);
// set up date formatting parameters
const options = {year: 'numeric'};
options.month = options.day = '2-digit';
// print date to console
const dateFormat = new Intl.DateTimeFormat([], options);
console.log(dateFormat.format(date));
Upvotes: 3
Reputation: 2669
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
Upvotes: 8
Reputation: 1314
Just for fun, here's how you could do it with moment.js
.
moment('2017-05-05').add(13, 'day').format('MM/DD/YYYY');
Upvotes: 3
Reputation: 914
var date = new Date('Mon Aug 14 2017 00:00:00 GMT-0500 (CDT)');
var newdate= (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Upvotes: 8