javascriptdatedatetimeexifexif-js

Javascript: How to convert exif date time data to timestamp?


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.


Solution

  • 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);