-2

Hello I was wondering is it possible to transform this Date string:

Sat Aug 12 2000 00:00:00 GMT+0200 (Midden-Europese zomertijd)

to the format of:

2000-08-12

I tried the following things:

var DateInput = "Sat Aug 12 2000 00:00:00 GMT+0200 (Midden-Europese zomertijd)";
var test = new Date(DateInput);
console.log(test);

var convert_date = test.toString().split('-');
console.log(convert_date);

But this does not work. Can anyone explain why and help me guiding to the right direction?

benvc
  • 14,448
  • 4
  • 33
  • 54
Rotan075
  • 2,567
  • 5
  • 32
  • 54
  • 2
    `test.getFullYear() + "-" + test.getDate() + "-" + ( test.getMonth()+1)` – Calvin Nunes Aug 23 '18 at 12:30
  • 1
    You can't just do `new Date` that returns different results depending on locations, browser, etc. etc. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Liam Aug 23 '18 at 12:46

4 Answers4

3

Looks like you are really just trying to reformat the input date string (without converting to local time). Your attempt relies on Date for string parsing, which is not recommended (see JavaScript Date note regarding date string parameters). It also uses toString which is implementation dependent and can't be relied on to reformat the output to a specific format like yyyy-mm-dd.

You can't just convert the date string to a Date object and then rely on methods like getDate() because those methods return values in local time which may be different than the date values you are trying to preserve from the input date string with a specific timezone.

As noted in other answers, there are lots of libraries you can use for this sort of thing. Otherwise, here are a couple of hacks you can try (beware, variations in your date string input format could break either of these approaches - hence the existence of the libraries).

Credit to @SergeyMetlov for the two digit month and date formatting.

If your date strings are consistently formatted in the same way as your example, just parse the string and reformat the output as desired.

const str = 'Sat Aug 12 2000 00:00:00 GMT+0200 (Midden-Europese zomertijd)';
const format = (d) => (d < 10 ? '0' : '') + d;
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

// parse date string (lazy since time parts not used)
const [day, m, d, yyyy, ...time] = str.split(' ');

// two digit format for month and date
const mm = format(months.indexOf(m) + 1);
const dd = format(d);

// format date as yyyy-mm-dd
const date = `${yyyy}-${mm}-${dd}`;
console.log(date);
// 2000-08-12

Or if you want to force the js date object to submit, then you can try something ugly like the below. The example relies on Date.parse to parse the input date string which is not recommended since implementations may not be consistent across browsers. If you really wanted to do something like this you should replace Date.parse (and the clunky substr line) with some better parsing of your own.

const str = 'Sat Aug 12 2000 00:00:00 GMT+0200 (Midden-Europese zomertijd)';
const format = (d) => (d < 10 ? '0' : '') + d;
const utc = Date.parse(str);
const localoffset = new Date().getTimezoneOffset() * 60000;
const operator = str.substr(28, 1);
const stroffset = (() => {
  let offset = str.substr(29, 4) * 60000;
  if (operator === '-') {
    offset *= -1;
  }
  
  return offset;
})();

// convert utc to "wrong" date, will look "right" when formatted
const wrong = new Date(utc + localoffset + stroffset);
const y = wrong.getFullYear();
const m = format(wrong.getMonth() + 1);
const d = format(wrong.getDate());
const output = `${y}-${m}-${d}`;
console.log(output);
// 2000-08-12
benvc
  • 14,448
  • 4
  • 33
  • 54
0

This should work test.getFullYear() + "-" + test.getMonth()+1 + "-" + test.getDate()

Shanil Arjuna
  • 1,135
  • 10
  • 18
0

Use date-fns to manipulate dates, its very cool modern JavaScript date utility library

Ex:

var format = require('date-fns/format');
var result = format(new Date('Sat Aug 12 2000 00:00:00 GMT+0200 (Midden-Europese zomertijd)'),'YYYY-DD-MM');

link: https://date-fns.org/v1.29.0/docs/format

Bhimashankar Mantur
  • 189
  • 1
  • 3
  • 12
  • You can't just do `new Date` that returns different results depending on locations, browser, etc. etc. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Liam Aug 23 '18 at 12:45
0

I actually created this dateFormat.js to handle exactlly this sort of thing. It extends the Date object so as long as you have included the file in your site you can use

var date = new Date(); 
date.formatDateTime('YDM')

And by using the Date object, you can also set the date you wish to format as normal.

Ajaypayne
  • 517
  • 3
  • 12
  • @Liam, while I would agree that it does appear good, moment.js does a lot more than just format dates, and for something this simple, it would be overkill to include the entire library. My answer gives a small amount of code that does the one thing that is being asked for. – Ajaypayne Aug 31 '18 at 10:59