JS Get the GMT stamp

Source: Internet
Author: User
Tags local time

Yesterday in a forum to see a friend asked JS how to get the GMT stamp.
Many friends first reaction is togmtstring ...
You can actually get Greenwich mean time, but not timestamp .
Although I do not have a good way to get one step, but at least to obtain, and share with you, let everyone cheap laugh.

getTimezoneOffset can get the offset of the current time zone, in minutes,
Our time zone is GMT+0800, so switching back should be-8 .
Convert to minutes-8 * = -480
getTimezoneOffset method to get the exactly-480

var dt = new Date;console.log (Dt.gettimezoneoffset ()); -480

We want to get the corresponding GMT stamp, just add-480 minutes.

var dt = new Date;dt.setminutes (dt.getminutes () + dt.gettimezoneoffset ()); Current time (minutes) + Time zone offset (minutes) console.log ("GMT Stamp:", Dt.gettime ()), Console.log ("Display in local time format:", dt.tolocalestring ());

Well, share this little bit of knowledge today.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.