0

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)
codeX
  • 4,842
  • 2
  • 31
  • 36
  • What javascript statement are you using to output the string? – Jonathan M Jan 03 '18 at 17:30
  • Just i used console.log(output) for testing purpose . – codeX Jan 03 '18 at 17:32
  • I presume your IDE is using Node to run JavaScript, not your browser. – Álvaro González Jan 03 '18 at 17:32
  • If that is the case , Any possible way to achieve the similar result via IDE ? – codeX Jan 03 '18 at 17:37
  • Can you try `d.toString()` in the IDE and see what gives? – Deepak Kamat Jan 03 '18 at 17:58
  • Using d.toString() produces UTC output only. – codeX Jan 03 '18 at 19:49
  • @downvoter : May I know , Why the question is downvoted ? – codeX Jan 03 '18 at 19:49
  • 2
    The output of *Date.prototype.toString* is **entirely** implementation dependent. That it happens to be in your preferred format is just good luck. See [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|1539.8132) – RobG Jan 03 '18 at 22:49
  • @arunprakashpj it is because you have got a lot of good suggestions and answers, still you are looking for an exact code to produce your desired output which is not possible as RobG said the direct output is implementation dependent. – Deepak Kamat Jan 04 '18 at 04:22
  • @DeepakKamat : The forum is open for discussion and to share knowledge.For sure, I thank you for the answer/suggestion/view posted by you but Its unfair to down vote a question just because I didn’t mark 'accept' to your suggestion. You down voted within two hours time of posting the question. By that time only few comments were here . In spite of me finding the solution using the suggestions , the valuable comments here may help future users who hunt solution for a similar question. – codeX Jan 04 '18 at 06:42
  • You do not seem to be trying out any suggestion / comments, but that's not even the reason I down-voted, it is for the fact that the question is very broad, and on top of that you didn't try anything to product your desired output with the suggestion to narrow down the topic. – Deepak Kamat Jan 04 '18 at 15:05

1 Answers1

0

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.

Deepak Kamat
  • 1,880
  • 4
  • 23
  • 38
  • Could you please share the code that displayed the above results ? and Do you have any idea, how to achieve this using date-fns? – codeX Jan 03 '18 at 17:35
  • The `Date` object let's you pass in some arguments to format it, but the option is very minimal https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date You have to try getting each part of the date object and place it in a string as required. – Deepak Kamat Jan 03 '18 at 17:38
  • Updated the answer. Please check. – Deepak Kamat Jan 03 '18 at 18:00
  • Using d.toString() produces UTC output only. – codeX Jan 03 '18 at 19:50
  • In my case, in Node REPL it gives me the output in the right format. I believe your best option is to manually use each part of the date object, and create a string that you require. – Deepak Kamat Jan 04 '18 at 04:21