0

I have this variable 'previousDate' and assigned it to previous date.

var previousDate;

When i do a console.log(previousDate), i get this value at the console.

Wed Dec 07 2016 10:02:37 GMT-0800 (Pacific Standard Time)

Now, when i json.stringify this data, i get following date and time.

2016-12-07T18:02:37.223Z

The date is the same but please note the time here. Instead of PST, it is showing me GMT. I need to have the PST timing here too when i stringify. Any suggestions please ?

2
  • What exactly is your goal? To transfer date objects, it is usually simpler and less error-prone to do so with a UTC timezone. Commented Dec 8, 2016 at 18:11
  • I want the PST timings to be displayed after stringify. but its showing UTC i guess... Commented Dec 8, 2016 at 18:12

3 Answers 3

2

When you create your object, create a method to return a date string.
Sample:

var obj = { d1: (new Date()), d1String: function() { return this.d1.toString(); } }

Or save the YourDate.toString() in one property of your object.

var objDate = { dateString: YourDate.toString() ;}

Or

var ff = function(YourObject) {  var OBJ = {} ;
for(x in YourObject)
{
   if (typeof(YourObject[x]) !== "function")
       eval("OBJ." + x + " = '" + YourObject[x] + "'");
    else
      eval("OBJ." + x + " = '" + YourObject[x]() + "'");
}
var strOBJ = JSON.stringify(OBJ);
return strOBJ;
}

My Gist on GitHub

https://gist.github.com/kiaratto/afb6bb9acd21d0dda37157eb3a92ec2f

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript date values don't contain timezone information.

When you log something to the console, the browser does its best to show you relevant and useful information. For dates, that often means that whatever UTC value is stored within is displayed in your local timezone, which for you must be "GMT-0800 (Pacific Standard Time)."

If you want to display the time in UTC, you can write:

console.log(date.toUTCString())

This is what JSON.stringify is doing under the hood; transporting dates with UTC helps simplify things when that information has to cross various timezones.

However, that behavior doesn't seem to be what you're looking for. If you want the string to be local-timezone when you send it, you can write:

console.log(date.toString())

Comments

1

It looks like you can get what you want, if you do:

date.toDateString() + ' ' + date.toTimeString()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.