37

The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00

Example 1: 2010-03-05T07:03:51-0800

Example 2: 2010-07-01T20:23:00-0700

I need to create a date object using these date strings. new Date() does not work on this string. Please help me convert these date strings into a date objects with the local timezone.

Thank you!

Edit: I am using this in Pentaho Data Integration 4.3.0.

vsync
  • 118,978
  • 58
  • 307
  • 400
Marina
  • 3,222
  • 5
  • 25
  • 35
  • it is a [rfc 3339](http://tools.ietf.org/html/rfc3339#section-5.8) format a profile of ISO 8601. `new Date("2010-07-01T20:23:00-0700")` works for me in Firefox/Chrome. – jfs Aug 02 '12 at 03:23
  • I am using it in Pentaho Data Integration. – Marina Aug 02 '12 at 11:37

3 Answers3

37

Take my timezone as an example (AEST):

function parseDate(str_date) {
  return new Date(Date.parse(str_date));
}


var str_date = "2015-05-01T22:00:00+10:00"; //AEST time
var locale_date = parseDate(str_date);

locale_date: Fri May 01 2015 22:00:00 GMT+1000 (AEST)

var str_date = "2015-05-01T22:00:00+00:00" //UTC time
var locale_date = parseDate(str_date);

locale_date: Sat May 02 2015 08:00:00 GMT+1000 (AEST)

Leo
  • 383
  • 3
  • 7
22

You can use a library such as Moment.js to do this.

See the String + Format parsing.

http://momentjs.com/docs/#/parsing/string-format/

The following should parse your date you provided, but you may need to modify it for your needs.

var oldDate = "2010-03-05T07:03:51-0800";

var dateObj = moment(oldDate, "YYY-MM-DDTHH:mm:ssZ").toDate();

Alternatively, see Moment's String parser, which looks like it is in the format you provided, with the exception of a space between the seconds of the time and the time zone.

http://momentjs.com/docs/#/parsing/string/

Alternative

A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com

Andrew M
  • 4,208
  • 11
  • 42
  • 67
  • It's not working for me. It takes my current timezone only in Date object. Please help – Jay Shukla Jun 19 '15 at 06:54
  • 4
    This shouldn't be the correct answer, if the question says "with timezone". Moment doesn't consider the timezone offset, instead uses the system's timezone. Why would you use a library for something that can also be done by parsing date using native JS. – Shobhit Srivastava Jun 08 '17 at 09:21
14

Date String:

var strDate = "2010-07-01T20:23:00-0700";

To local time representation in native JS Date object:

var ltzDate = (new Date(strDate)).toLocaleString();

Joe Johnson
  • 1,814
  • 16
  • 20
  • Does not work for me. (I am using this in Pentaho Data Integration) – Marina Aug 02 '12 at 11:42
  • Sorry about that. I tested this in Chrome (Developer Tools console), and it worked fine for me. Since this is standard ECMAScript (JavaScript), I assumed it would work everywhere. – Joe Johnson Aug 03 '12 at 00:01
  • It's not work in safari. you should use this format "2010-07-01T20:23:00-07:00" with : – Iman Shafiei Oct 20 '20 at 09:15