9

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);
Gavin Morrow
  • 711
  • 1
  • 6
  • 19
anatp_123
  • 1,165
  • 2
  • 10
  • 25

4 Answers4

8

var date = new Date('Mon Aug 14 2017 00:00:00 GMT-0500 (CDT)');
var newdate= (date.getMonth() + 1) + '/' + date.getDate() + '/' +  date.getFullYear();
samnu pel
  • 914
  • 5
  • 12
  • 6
    Is this really the correct answer? It does not have preceding zero for one digit month or day. Example that I get is `5/1/1993` and this is not mm/dd/yyyy – Undefined function May 10 '21 at 18:02
4

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

Vijay Jagdale
  • 2,321
  • 2
  • 18
  • 16
  • sorry, but toLocaleDateString does NOT seem to reliably produce m/d/yyyy or mm/dd/yyyy under nodejs for *any* arguments to pass that I can find. – Michael Jun 13 '23 at 23:16
  • Can you give me some example? I tested several dates in Node.js v18.16.0 and they gave me right formats every time. – Vijay Jagdale Jun 14 '23 at 03:05
  • You've got a newer version of nodejs than we are using in production. From my research, apparently it's implementation dependant so the fact that it changed across the last 10 node versions isn't surprising. – Michael Jun 14 '23 at 06:04
3

Just for fun, here's how you could do it with moment.js.

moment('2017-05-05').add(13, 'day').format('MM/DD/YYYY');

fiddle link

JavaKungFu
  • 1,264
  • 2
  • 11
  • 24
2

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 date = 'Fri Dec 29 2017 00:00:00 GMT-0800 (Pacific Standard Time)';
const dateSet = new Date(date);
const fiveDaysLater = dateSet.setDate(dateSet.getDate() + 5);

// set up date formatting parameters
const ops = {year: 'numeric'};
ops.month = ops.day = '2-digit';

console.log(new Intl.DateTimeFormat([], ops).format(fiveDaysLater));
omikes
  • 8,064
  • 8
  • 37
  • 50