Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery => toUTCString() returns 1 day back date

I need to get format like below

19 Aug 2015 04:22:36 GMT

I have following code

var dt = '2015-08-19 04:22:36';
alert(new Date(dt).toUTCString().substr(4))

It returns me

18 Aug 2015 22:52:36 GMT

where as it should return

19 Aug 2015 22:52:36 GMT

What is wrong in my code

JsFiddle

As per answers below it seems it's converting the date to UTC date time.

I have date in UTC format in database. So please if some one could suggest the desired format without using toUTCString()

Update

Tried following

var dt = '2015-08-19 04:22:36  UTC';

alert(new Date(dt).toUTCString().substr(4))

It gives me liddate in FireFox and IE, chrome it is fine

Solved changed date string to

var dt = '2015/08/19 04:22:36  UTC';

Thanks

like image 372
Md. Parvez Alam Avatar asked Mar 23 '26 09:03

Md. Parvez Alam


1 Answers

The toUTCString() converts your date to UTC so the outcome is correct! Change your string to this var dt = '2015-08-19 04:22:36 UTC'; (notice the UTC)

or use the Date.UTC() function:

alert(new Date(Date.UTC(2015, 09, 19, 04, 22, 36)).toUTCString().substr(4))

Notice that the month is 0 based (0 -11) so to get August you need to increase your monty by 1

like image 114
Adam Bilinski Avatar answered Mar 25 '26 22:03

Adam Bilinski