1

So I have a code in nodejs where I am getting a current date and time.

var timestamp = new Date().toString();

My output looks like this:

Wed Nov 09 2016 16:02:32 GMT+0100 (CET)

Can anybody please give me an advice how to get rid of GMT+0100 (CET) in my output?

3
  • Do you still want the time to be in CET, just without the 'GMT+0100 (CET)' bit at the end, or do you want the current date in UTC? Commented Nov 9, 2016 at 15:07
  • You want to get the date-time instance without information about the timezone, or a textual representation of said date-time instance without the timezone? Commented Nov 9, 2016 at 15:09
  • Just a textual representation should not be with timezone, otherwise everything is right. I just want to "hide" GMT+0100 (CET) Commented Nov 9, 2016 at 15:16

1 Answer 1

3

If you want the UTC date/time then use toUTCString():

new Date().toUTCString();
// 'Wed, 09 Nov 2016 15:11:53 GMT'

If you want a standard UTC ISO8601 timestamp use toISOString():

new Date().toISOString()
// '2016-11-09T15:13:00.380Z'

If you literally just want to get rid of GMT+0100 (CET) then:

new Date().toString().replace(' GMT+0100 (CET)', '');
// 'Wed Nov 09 2016 15:15:05'

or:

var now = new Date()
now.toString().substr(0, now.toString().indexOf(' GMT'))
// 'Wed Nov 09 2016 15:15:05'

Which will work for all timezones.

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

7 Comments

Awesome! thank you the last option was that one I have been looking for. Cheers!
No problem! If that's what you were after then I've updated my answer with a solution than should work for all timezones.
One more little question. How do I now order timestamps so I see the most recent? ORDER BY datetime(timestamp) seems to not work for me :/
Are you saving them into a database and wanting to do this in SQL? Or is this still in node.js?
From the SQLite docs it looks like it could be an issue with the date formatting, but it's probably better to create a new question for that. If you're saving that timestamp to a DB then the ISO8601 timestamp might be better. If you're displaying the date to a user then you can convert ISO8601 to the local timezone in the client, this way you don't have to worry about time zones, day light saving, etc. Just a suggestion :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.