Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

javascript - 如何将JavaScript日期转换为UTC?(How do you convert a JavaScript date to UTC?)

Suppose a user of your website enters a date range.(假设您网站的用户输入了日期范围。)

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but the server expects all dates and times to be in UTC.(您需要将此日期发送到服务器进行某些处理,但是服务器希望所有日期和时间都采用UTC。)

Now suppose the user is in Alaska or Hawaii or Fiji.(现在,假设用户位于阿拉斯加,夏威夷或斐济。)

Since they are in a timezone quite different from UTC, the date range needs to be converted to something like this:(由于它们所处的时区与UTC截然不同,因此需要将日期范围转换为以下形式:)
2009-1-1T8:00:00 to 2009-1-4T7:59:59

Using the JavaScript Date object, how would you convert the first "localized" date range into something the server will understand?(使用JavaScript Date对象,您如何将第一个“本地化”日期范围转换为服务器可以理解的范围?)

  ask by dthrasher translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simple and stupid(简单而愚蠢)

var date = new Date(); 
var now_utc =  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
 date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

 return new Date(now_utc);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...