Envin
Envin

Reputation: 1523

Removing "(Eastern Standard Time)" from javascript date

I have this function

//--format ISO8601 date into sections
function formatDate(date){
    var a = date.split(/[T]/);
    var d = a[0].split("-"); // date 
    var t = a[1].split(":"); // time
    t[2] = t[2].split("-"); // Remove Time zone offset 
    var formattedDate = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2][0]);
    //formattedDate.replace(/ *\([^()]*\) */g, "");
    return formattedDate;
}

which returns a date that looks like this

Tue Jan 15 2013 11:07:14 GMT-0500 (Eastern Standard Time)

I want to remove the (Eastern Standard Time) part. I tried doing formattedDate.replace, but it won't work because I believe it isn't a String.

Any suggestions?

EDIT:

My desired output is

Tue Jan 15 2013 11:07:14 GMT-0500

Upvotes: 2

Views: 2150

Answers (1)

pimvdb
pimvdb

Reputation: 154858

What you return is a Date instance. When you output it (by logging it etc), it is converted into a string. So what you want is .toString() and then use string functions:

var str = formattedDate.toString();

// this should be safe since nothing else in the date string contains a opening paren
var index = str.indexOf(" (");

// if the index exists
if(~index) {
  str = str.substr(0, index);
}

Upvotes: 3

Related Questions