Getting IST Time stamp using JavaScript - javascript

I wish to get the following timestamp as output :
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)
When I tried using the following code in browser,
var d = new Date();
Am getting the desired result. But whenever I run the snippet in my Webstorm and console the result, I am getting the result as
2018-01-03T17:11:53.093Z
Why such difference in format? Why is the result produced in the browser not reproducing in my IDE Console? How to produce the result in the exact format as
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)

I think that's something that the JavaScript engine does built inside of the IDE. Anyways the raw data when you console.log the date object isn't directly used generally so that is why there are differences in how different engines may output it.
The IDE's JavaScript engine is most certainly outputting the date object similar to when it is output using DateObject.toISOString() in a browser which produces something like 2018-01-03T17:55:37.048Z
To fix that you can tell it to output it differently, like this
var d = new Date();
console.log( d.toString() );
I tried it on Node and seems to work pretty well, the IDE is probably using Node so that's going to work fine with it as well.
OR Use momentjs or a similar library to normalize how you want to output a time object
While you are getting the output in the format
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)
I am getting it as
Wed Jan 03 2018 23:01:41 GMT+0530 (India Standard Time)
Not much different, still you can see the difference in how IST is shown. Probably browsers also take the settings of your operating system in consideration when outputting a raw time object.

Related

convert date string to ISO date with timezone

I have a collection of dates and times, it is formatted like this:
01.07.2013 16:10.
I know I have to rearrange to match ISO standard, so I managed to end up with a string like this:
2013-07-01T16:10.
From this, I need to create a Date Object. After reading many questions on here about this I am a little confused about how to add a timezone to this. All of the times are in New York Cities local time, which I also want to store the dates in.
So since Javascript takes the timezone from my machine, I changed it to be the one from NYC. However, the following part confuses me the most:
> print(new Date("2013-07-01T16:10"))
Mon Jul 01 2013 16:10:00 GMT+2000 (EDT)
Question: Why does it say GMT+2000 when my local time is GMT-0400 (the one of NYC)? Is this 'correct'? If not, how is this done properly?
--Updates:
I am using mongoDB 3.4.4 and interpreter Version is MozJS-38. The systemsetup -gettimezone returns America/New_York. Running mongo-express in chrome shows: Mon Jul 01 2013 06:00:00 GMT+0200 (EDT) for ISODate("2013-07-01T04:00:00.000Z") stored, but the shell says Tue Jul 02 2013 00:00:00 GMT+2000 (EDT) for print(new Date("2013-07-01T04:00:00.000Z"));.
You didn't specify the time zone offset when creating the Date object.
> new Date("2013-07-01T16:10-04:00")
2013-07-01T20:10:00.000Z

Milliseconds to specific date format in javascript

Hello All, I have time in milliseconds and I want convert it into "dd/mm/yyyy hh:mm:ss"
I have tried the following code in javascript but I failed to do it, What I get is "Thu Jul 07 2016 16:22:10 GMT+0530 (IST)" this full timestamp, Code I tried is as follows:
self.users[i].lastLoggedIn = (new Date(self.users[i].lastLoggedIn)).toString("mm/dd/yyyy hh:mm:ss");
Where am I going wrong? for reference I have attached one screen shot of the same. Thank you..!!!
I suggest using moment.js for handling dates in Javascript. It is available for browsers, server-side engines and so on.
moment(self.users[i].lastLoggedIn).format("mm/dd/yyyy hh:mm:ss")

Firefox vs Chrome Date Difference with momentJS

I have a function that gets the date in the DATETIME format:
2015-06-18 00:00:00
Doing moment.utc("2015-06-18 00:00:00").toDate() will display different results in Firefox and Chrome:
Firefox: Date 2015-06-18T00:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
Also, using new Date("2015-06-18 00:00:00") will return Invalid Date in Firefox, but adding a "T" before the hours will fix that issue. But then if I do new Date("2015-06-18T00:00:00") will return:
Firefox: Date 2015-06-17T21:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
It's driving me nuts.
How can I get both browsers to show the same hour?
How can I get Firefox to display the result in Chrome's format?
The whole point of using a dedicated date library is to obtain transparent cross-browser date features. However, as soon as you run .toDate() you get back the native Date object. If you then convert it to string by using the builtin Date.toString() method you've finally dropped all the library goodies and got back to vanilla JavaScript.
Tips:
Don't use strings expect for display purposes
Use the library features to generate those strings
try
new Date("2015-06-18T00:00:00").toString()
looks the same on both for me

Is the output of Date.toString() always in the same format?

As the title asks, is the output of Date.toString() (more precisely, Date.toTimeString()) always in the same format, in all browsers?
I ask this since the EMCAScript specification says that the "contents of the String are implementation-dependent".
I need to make sure of this because I need to format the string by inserting HTML span elements as follows:
(new Date()).toTimeString().replace(" GMT", "<span id='offset'> GMT") + '</span>' );
This would produce (in Google Chrome v28.0.1500.95) something like
18:19:26<span id="offset"> GMT-0700 (Pacific Daylight Time)</span>
which I can then style with CSS.
Suggestions for better ways to style the output would also be great!
In brief, no, toString does not always output the same format.
new Date().toString()
prints
"Sat Feb 11 2012 02:15:10 GMT+0100" in Firefox 10
"Sat Feb 11 2012 02:18:29 GMT+0100 (W. Europe Standard Time)" in Chrome 16
"Sat Feb 11 02:18:59 UTC+0100 2012" in Internet Explorer 9
I know these are older browser version, but it shows that it is browser dependent.
However, when using toTimeString() it appears it always starts with hours:minutes:seconds[space]...
Therefore, you could instead split the string into 2 portions based on the first [space] with:
indexOf(" ")
Then wrap the second portion with your span
Moment does some string formatting of dates, but it also does not handle your requested offset string very well as it depends on the toString() method.
I hope that helps
Moment.js is a nice library for formatting dates in JS. https://github.com/moment/moment
Otherwise yes, you'll end up parsing it yourself as browsers render this differently.

Javascript date formatting - one hour out due to daylight saving

So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)

Categories

Resources