0

I'm trying to convert a date in string format to a Date object in JavaScript. The date is coming from the openweathermap.org API, which gives the date string in the following format:

'2018-04-28 09:00:00' or 'yyyy-mm-dd hh:mm:ss'

I know that the JavaScript Date class can take other string formats of dates, but this one returns NaN when I try anything with it. How can I convert a date string, like the one above, and convert it easily to a JavaScript object? Thanks!

John Seed
  • 357
  • 1
  • 7
  • 18
  • Where did it return `NaN`? Which environment? – Hassan Imam Apr 28 '18 at 06:09
  • React-native project on Windows 10, running app on Android device. – John Seed Apr 28 '18 at 06:13
  • Because of the variances in parsing of date strings, it is recommended to always manually parse strings as results are inconsistent, especially across different ECMAScript implementations where strings like "2018-04-28 09:00:00" may be parsed to as NaN, UTC or local timezone. – Martin Apr 28 '18 at 06:35

2 Answers2

1

Since you are getting NaN while directly converting the string to date. You can split the string on spaces, - and : and then pass the value to date constructor and generate the date object.

const str = `2018-04-28 09:00:00`;
const [date, time] = str.split(' ');
const [year, month, day] = date.split('-');
const [hh, mm, sec] = time.split(':');
const dateObj = new Date(year, month - 1, day, hh, mm, sec);
console.log(dateObj);

As pointed out by @RobG, this could also be done using the regex.

const str = `2018-04-28 09:00:00`;
var b = str.split(/\D/);
var date = new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);
console.log(date);

const str = `2018-04-28 09:00:00`,
      date = new Date(...(str.split(/\D/).map((v,i)=>i==1?--v:v)));
console.log(date);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • A lot easier to split on non-digits: `var b = str.split(/\D/)` then `new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);` and you're done, and compatible with every implementation ever. ;-) – RobG Apr 28 '18 at 06:42
  • Ok, that's working now. Thanks! – John Seed Apr 28 '18 at 06:46
  • Or if you want to use new stuff and don't care about being inefficient, then `new Date(...(str.split(/\D/).map((v,i)=>i==1?--v:v)))` does the job in less code, but is much less compatible. – RobG Apr 28 '18 at 06:49
0

Just try new Date(str)

d = new Date("2018-04-20 09:00:00")
Fri Apr 20 2018 09:00:00 GMT+0800 (Hong Kong Standard Time)
d.getDate()
20

ref: https://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format

iloahz
  • 4,491
  • 8
  • 23
  • 31
  • When I do what you suggest and print the result of `d` to the console, it says 'Invalid Date'... Is this date format not recognized by Date? – John Seed Apr 28 '18 at 06:27
  • That format isn't supported in ECMA-262. Using the built-in parser is not recommended, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 28 '18 at 06:43
  • @JohnSeed—parsing of any format other than the one specified in ECMA-262 is implementation dependent. An implementation may decide that is an invalid attempt at the specified format, or it might attempt to parse it to a Date. Both outcomes are consistent with the language specification (e.g. Safari does the former, Chrome the latter, both are based on WebKit). – RobG Apr 28 '18 at 06:45