javascript - Javascript如何在使用时区时逐日验证getDay
问题描述
我正在尝试验证一周中的哪一天等于星期三 (3),如果我按照以下方式进行操作,效果会很好。
var today = new Date();
if (today.getDay() == 3) {
alert('Today is Wednesday');
} else {
alert('Today is not Wednesday');
}
但我无法对时区做同样的事情。
var todayNY = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
if (todayNY.getDay() == 3) {
alert('Today is Wednesday in New York');
} else {
alert('Today is not Wednesday in New York');
}
解决方案
正如函数“toLocaleString”所暗示的那样,它返回一个字符串。'getDay' 存在于 Date 类型上。
因此,要使用“getDay”,您需要将字符串转换回日期。
尝试:
var todayNY = new Date().toLocaleString("en-US", {
timeZone: "America/New_York"
});
todayNY = new Date(todayNY);
if (todayNY.getDay() == 3) {
alert('Today is Wednesday in New York');
} else {
alert('Today is not Wednesday in New York');
}
推荐阅读
- google-chrome-extension - chrome.tabs.getCurrent 返回未定义
- python-3.x - 改变先知情节的特点
- c# - 在 AutoCAD 中打开一个 dwg 文件并通过 C# 滚动到 dwg 地图的特定位置
- python - 什么是属性?它如何“保持”一个价值?
- html - 如果在 d3.js 中有阴影,则在 svg 中的 svg 中的 ForeignObject 中的选择框不会重新绘制/更新
- android - 使用 Dagger 时将数据传递给 ViewModel
- python - AttributeError: 'Event' object has no attribute 'setEvent'
- sql - 如何在相对论中查询样本?
- regex - Google 表格 ImportXML 以显示标题并嵌入 URL
- c++ - Qt 如何使用 mouseMoveEvent 移动视图