简体   繁体   English

从 `const now = new Date()` 开始,如何验证我是否早于或等于给定时区和小时的特定日期?

[英]from `const now = new Date()`, how can I verify that I am before or equal a particular date at a given timezone and hour?

I have a timezone map with publishing hour in the local zone with news that must define when they should be published on a date using a date picker.我有一个时区地图,在本地区域发布时间,新闻必须使用日期选择器定义何时应该在某个日期发布。

This is a new news article that is initialized with the following:这是一篇新的新闻article ,初始化如下:

  • { timeZoneId: 'Europe/Paris, releaseHour: 9, publishingDateTime: undefined } // 9 is the hour GMT+1 { timeZoneId: 'Europe/Paris, releaseHour: 9, publishingDateTime: undefined } // 9 是 GMT+1 小时

I want to know how can I from const now = new Date() , verify if this article should be published today or the next day, the criteria are:我想知道我如何从const now = new Date() ,验证这篇文章应该今天发布还是第二天发布,标准是:

  • Is now before releaseHour ? now是在releaseHour之前吗? (is 9am GMT+1 in paris already passs or not) (巴黎的格林威治标准时间上午 9 点是否已经过去)
  • If yes, then we should offer the next release slot at 9am GMT+1 + 1 day如果是,那么我们应该在格林威治标准时间上午 9 点+1 + 1 天提供下一个发布时段
  • If no, then we should use the release slot at 9am the same day如果没有,那么我们应该在当天上午 9 点使用发布槽

How is this possible?这怎么可能?

This is how I have tried:这是我尝试过的方式:

import { isBefore, isEqual } from 'date-fns';
import { utcToZonedTime } from 'date-fns-tz';

export const getNextPublishingDateTime = (now, timeZoneId, releaseHour) => {
  const zoned = utcToZonedTime(now, timeZoneId);
  const releaseTime = new Date(zoned.toISOString());
  releaseTime.setHours(releaseHour, 0, 0, 0);
  if (isBefore(zoned, releaseTime) || isEqual(zoned, releaseTime)) {
    console.log('before');
    return releaseTime;
  }
  releaseTime.setDate(releaseTime.getDate() + 1);
  console.log('after');
  return releaseTime;
};

But the hour returned by utcToZonedTime is not +01:00 offset, instead it is a date at my offset.但是utcToZonedTime返回的小时不是 +01:00 偏移量,而是我偏移量处的日期。

I have tried some other ideas, using moment-tz and vanilla Date , I found this task a bit complicated and hope to find help with the JS community as this look to be a normal date comparaison.我尝试了一些其他的想法,使用moment-tz和 vanilla Date ,我发现这个任务有点复杂,希望能在 JS 社区中找到帮助,因为这看起来是一个正常的日期比较。

You can use the Intl object and formatToParts method to get the current time in any supported timezone using IANA representative locations .您可以使用 Intl 对象和formatToParts方法获取使用 IANA代表位置的任何受支持时区中的当前时间。 It's reasonably well supported .得到了相当好的支持

Eg例如

 function getHour(date, loc) { let d = new Intl.DateTimeFormat("en-EN", { hour: 'numeric', hour12: false, timeZone: loc }); return d.formatToParts(date)[0].value; } let loc = 'Europe/Paris'; let now = new Date(); console.log(`The current hour in ${loc} is ${getHour(now, loc)}.`);

The above is just for illustration, there should be validation of input and return values.以上只是为了说明,应该有输入和返回值的验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个 Date 对象,如何在特定时区的上午 9:00 获取此原始 Date 的另一个 Date 对象(或其时间戳)? - Given a Date object, how can I get another Date object (or its timestamp) of this original Date, but at 9:00am of a specific timezone? 如何使用 Node.js 获取 Date.now() 的年、日、分、时和秒 - How can I get the year, day, minute, hour, and second of Date.now() using Node.js 如何在jquery中使用特定时区的php脚本中的值创建新的Date()? - how can I create a new Date() in jquery with a value from my php script in a specific timezone? 如何解释 Date 的这种时区行为? - How can I explain this timezone behavior of Date? 为什么我不能从给定的字符串创建一个新的Date()? - why I can't create a new Date() from given string? 给定一个时区字符串,如何在JS中将该日期对象设置为该TZ - given a timezone string, how can i set a date object to that TZ in JS 如何制作一个读取日期(小时,分钟,秒)的程序,然后从第二个日期中减去它 - How can I make a program that reads the date(hour,minutes,seconds) then it subtracts it from the second date 如何在用户Javascript数字时钟的新Date()中使用服务器时区? - how can i use server timezone in new Date() of the user javascript digital clock? 如果我设置与给定查询字符串参数相关的日期,我如何获得正确的星期几? - How can I get the right day of the week, if I am setting the date in relation to the given query string parameters? 在给定 php 中的数组信息的情况下,如何在 javascript 中使用 new Date() 创建日期? - How do I make a date with new Date() in javascript given information from an array in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM