计算日期选择器+时间选择器是否在东部标准时区的过去 [英] Calculating if date picker + time picker are in the past of Eastern Standard Time zone

查看:62
本文介绍了计算日期选择器+时间选择器是否在东部标准时区的过去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准输入type = date字段,该字段在发布时会输出诸如 2016-03-28的值,而时间选择器在发布时会输出诸如 10:30 pm的值。我需要检查日期和时间的组合是否相对于EST(东部标准时区)而言在过去。最好的方法是什么?

I've got a standard input type=date field that outputs a value such as "2016-03-28" when its posted, and a time picker that outputs a value like "10:30pm" when posted. I need to check if the combination of the date and time are in the past relative to the EST (eastern standard time zone). What's the best way to do this?

推荐答案

为日期(如2016-03-28)和时间(如晚上10:30)分别输入您应该同时解析两者以产生Date。要将日期视为EST(大概是美国-05:00),请使用UTC方法创建日期并添加5小时。

Given separate inputs for date like 2016-03-28 and time like 10:30pm you should parse both to produce a Date. To treat the date as EST (presumably US -05:00), create the date using UTC methods and add 5 hours.

现在的日期将有一个UTC时间值,因此您可以按原样使用它,然后直接比较Date对象:

A Date for "now" will have a UTC time value so you can use that as–is, then just directly compare the Date objects:

/* @param {string} ds - date string in format yyyy-mm-dd
** @param {string} ts - time string in format hh:mmap
** @returns {Date} Date with time value for equivalent EST time
*/
function parseDateTimeAsEST(ds, ts) {
  var b = ds.split(/\D/);
  var c = ts.split(/\D/);
  c[0] = +c[0] + (/pm$/i.test(ts)? 12 : 0);
  return new Date(Date.UTC(b[0], b[1]-1, b[2], c[0]+5, c[1]));
}

// Is now before or after 2016-03-28T22:30:00-05:00?
document.write(new Date() < parseDateTimeAsEST('2016-03-28','10:30pm')? 'before' : 'after');

document.write('<br>');

// Is now before or after 2016-04-01T01:30:00-05:00?
document.write(new Date() < parseDateTimeAsEST('2016-04-01','01:30am')? 'before' : 'after');

这篇关于计算日期选择器+时间选择器是否在东部标准时区的过去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆