7

In javascript, while using exif-js to extract metadata of an image file, I am getting date time format as 2017:03:09 14:49:21.

The value in the DateTimeOriginal property is formatted as YYYY:MMY:DD HH:MM:SS. When I use var d = new Date(2017:03:09 14:49:21), it returns NaN. It's the colons in between the YYYY, MM, and DD which causes problem.

How to solve this problem?

Thanks in advance.

Manthan Sheth
  • 75
  • 1
  • 5

3 Answers3

7

Don't use the built-in parser (i.e. Date constructor or Date.parse) for parsing strings as it's largely implementation dependent and unreliable. If you can trust the date to be valid, then the following will do:

/* Parse date string in YYYY-MM-DD hh:mm:ss format
** separator can be any non-digit character
** e.g. 2017:03:09 14:49:21
*/
function parseDate(s) {
  var b = s.split(/\D/);
  return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);
}

console.log(parseDate('2017:03:09 14:49:21').toString());

It's fairly easy to add validation to the values. Otherwise, use a library and make sure you specify the format to parse.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • That's great, Thank you :) – Manthan Sheth Mar 29 '17 at 14:34
  • THIS IS THE ANSWER but in Node.js you can use the accepted answer. OR const getDate = (s) => { const [year, month, date, hour, min, sec] = s.split(/\D/) return new Date(year,month-1,date,hour,min,sec) } – zavr Mar 04 '19 at 09:15
6

My recommendation would be to use Moment (http://momentjs.com/docs/), as it provides clean parsing of dates. With Moment, what you want is this:

var tstamp = moment("2017:03:09 14:49:21", "YYYY:MM:DD HH:mm:ss");
var date = tstamp.toDate();
Alain1405
  • 2,259
  • 2
  • 21
  • 40
Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    Thank you for your response. Is there any other way to do it? Coz I don't want to load a whole js for just one array, unless it is the only way. – Manthan Sheth Mar 29 '17 at 04:09
  • 1
    Answers that are just "use library X" are not particularly helpful. Parsing the OP's format is 2 lines of code. – RobG Mar 29 '17 at 05:12
  • 1
    If you want to parse correctly the EXIF date, you need to use this parsing syntax: "YYYY:MM:DD HH:mm:ss". – Félix Veysseyre Dec 28 '17 at 22:33
3

You can do simple string manipulation and create date if the format is always the same, as:

var str = "2017:03:09 14:49:21".split(" ");
//get date part and replace ':' with '-'
var dateStr = str[0].replace(/:/g, "-");
//concat the strings (date and time part)
var properDateStr = dateStr + " " + str[1];
//pass to Date
var date = new Date(properDateStr);
console.log(date);
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Great. Thanks :) – Manthan Sheth Mar 29 '17 at 04:17
  • And watch it fail in Safari. :-( It doesn't make any sense to parse the string only to generate a new string that must then be parsed (very unreliably) by the built-in parser. Just collect the digits and give them directly to the Date constructor (subtracting 1 from the month of course). – RobG Mar 29 '17 at 05:11