Json Date to JavasScript Date Conversion problem - javascript

Heres the code for converting the json retured date to a string of date.
String.toDate = function(stringDate) {
var newDate = new Date(parseInt(stringDate.replace("/Date(", "").replace(")/", ""), 10));
return newDate;
}
Here are the details:
Date from the database: 2009-11-18 03:23:25.107
Date Returned by JSON: "/Date(1258514605107)/"
Date Returned by the toDate function :
Wed Nov 18 2009 11:23:25 GMT+0800 (Taipei Standard Time)
Web Server and Database server time zone are the same.
Im wondering why the date becomes the current date in my timezone.
Is there anyone here encountered this kind of problem?

and what about your browser/OS settings ? you need the GMT time ?
I think you get get it with toUTCString a more complete reference about the date class
alert(newDate.toUTCString());

Related

comapre javascript toUTCString() with UTC date time

From Authentication API along with token i am getting its expiry date time which is in UTC
I am using npm jwt-decode package to extract the info
private setToken(value: string) {
this._token = value;
var decoded = jwt_decode(value);
this._expiry = new Date(decoded['expiry']);
}
To check if token expired i compare it with current UTC dat time
isTokenExpired() {
var currentUTC = new Date().toUTCString() as any;
var flag = this._expiry < (currentUTC as Date);
if (flag)
return true;
else
return false;
}
but new Date().toUTCString() gives strig instaed of date which always retuen false when compare with date object
I tried to convert it to date but after conversion it show date as per browser local time zone instead of actual utc date time
console.log('current UTC : '+ currentUTC);
console.log('expiry UTC : '+ this._expiry);
console.log('utc string to UTC Date : ' + this.createDateUTC(currentUTC));
console.log(new Date(Date.parse(currentUTC as string)));
console.log(this._expiry < (currentUTC as Date));
createDateUTC(dateUTC) {
return new Date(dateUTC + "Z");
}
output is as follow
current UTC : Fri, 02 Sep 2022 02:32:21 GMT
expiry UTC : Fri Sep 02 2022 03:32:08 GMT+0530 (India Standard Time)
utc string to UTC Date : Invalid Date
Fri Sep 02 2022 08:02:21 GMT+0530 (India Standard Time)
Current UTC is correct
expiry UTC is correct , Current UTC + 1 hours as expiry time returned from api
third one is converted to date which is invalid date , Ref : stack overflow Post by Killer
fourth one , tried to parse but it after parsing its local time. UTC time lost.
Any idea how to convert UTC string to date object without loosing its actual value, or any other approach to get current UTC date time
Update 1 :
After refresh
this._expiry = new Date(decoded['expiry'])
inside setTokn() method also lost UTC date time and convert it as per local time
Update 2:
Tried code by "HsuTingHuan" but got same result. Issue persist
I had met similar case before. Hope my solution can help you
I was used the javascript Date object's toISOString() function or JSON.stringnify()(Because the JSON object will always convert to ISO 8601 format)
And compare all of the datetime object by ISO 8601 format
example works on EDGE:
var currentUTC = new Date().toISOString();
console.log(currentUTC)
function createDateUTC(dateUTC) {
return new Date(dateUTC);
}
var currentUTCDateobj = createDateUTC(currentUTC)
console.log(currentUTCDateobj)
var expiry = new Date("2022-09-02 00:00:00")
console.log(expiry)
console.log(expiry < currentUTCDateobj);

How can I convert GTM-0006 to ISO without Adding hours

I'm working in a from who has a date field and by default it shows the current date.
I set the date using this:
var date = new Date(); = Tue May 25 2021 17:06:01 GMT-0600 (Mountain Daylight Time) {}**
Everything works fine, but when I send the data to the controller, the JSON automatically converts it to ISO and the date received by the controller is 6 hours in advance.
I understand a little bit the context about GMT-0006 (my current timezone is 6 hours more than the 0 timezone), and the fact that my controllers received the date in ISO format because when I converted to ISO format is the same problem
date.toISOString() = "2021-05-25T23:06:01.861Z" (6 hours in advance)
so my question is, there is a way to create a date that allows me to use .toISOString() and keep the same?
or create a date with my current hour but -0000 so when I convert it to toISOString keeps the same?

How to convert JSON time stamp in javascript to date and time?

I am getting Time stamp as "2020-03-02T05:50:31.000Z"
How to convert this to Normal Readable Format with Date and Time Zone
const parsedDate = new Date("2020-03-02T05:50:31.000Z");
console.log(parsedDate.toGMTString())
//"Mon, 02 Mar 2020 05:50:31 GMT"
console.log(parsedDate.toLocaleString())
//"3/2/2020, 11:20:31 AM"
console.log(parsedDate.toDateString(), parsedDate.toTimeString())
//Mon Mar 02 2020 11:20:31 GMT+0530 (India Standard Time)
In java-script above date format can be parsed by using Date.
Eg:
var date = new Date('2020-03-02T05:50:31.000Z');
I don't know what you mean readable format but you can use following methods:
new Date('2020-03-02T05:50:31.000Z').toLocaleString();
// outputs date according to user locale settings
Or you can use getYear, getMonth, getDay methods to get them and format date as you want
You can use moment.js for date time format and conversion.
Pass your date timestamp as below:
moment.parseZone("2020-03-02T05:50:31.000Z").format("DD-MM-YYYY hh:mm:ss z Z")
Modify format as you need ref mentioned here
Use in your code as mentioned here

Date is wrong when JS passed a Date in PHP

I am using Laravel 5.7 and I am having a problem with the date string being passed from JS to PHP.
Before I passed the date in Vue JS and I put a console.log() to see the actual value and I have this:
Fri Feb 01 2019 00:00:00 GMT+0800 (Philippine Standard Time)
and then when I receive it to Laravel Controller, I \Log::info() to show the value being received but I have this:
2019-01-31T16:00:00.000Z
does anyone encountered the same problem and managed to fix it? Please help
It's not different date, just ISO formatted when you convert date into string.
you can create the date object again using new Date()
let date = new Date()
console.log(date)
console.log(date.toString())
let date2 = new Date(date.toString())
console.log(date2)

Javascript New Date Changing Hour Value

I am receiving times in the an AJAX request and am converting them using the new Date() function.
I receive 2013-06-18T12:00:15Z
However, somehow I get the following after new Date():
Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)
Why is it not:
Tue Jun 18 2013 12:00
See the following demo:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
This is a time zone problem. You must be in the EDT timezone (GMT-0400). To correctly parse the date you should tell the parser in which timezone your date is correct.
For you parse your date like this :
new Date('2013-06-18 12:00:15 GMT-0400')
"GMT-0400" means GMT time minus 4 hours
Or if you don't wish to reformat your string, you can use the date.getUTC* functions to get the time as you parsed it.
The full list is available at Mozilla's documentation.
I agree with Vaim Caen's answer that this is a timezone issue, but not with parsing - the date is being parsed fine, but into your local timezone, while you're expecting it to be parsed into UTC date.
This answer shows how to convert from your current timezone to UTC - applying this to the TryIt demo gives:
var msec = Date.parse("2013-06-18T12:00:15Z");
// or: var msec = Date.parse("Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)");
var d = new Date(msec);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
document.getElementById("demo").innerHTML = d;
Edit: If you all you're interested in is displaying the date (no further manipulations) then you can use:
d.toUTCString()
which will show the date in GMT (for me it actually shows "GMT" so most likely not of use!)
The alternative is to add a function to the prototype to show the date in whatever format you want and use the date.getUTC* methods.

Categories