Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript将日期转换为本地时区_Javascript - Fatal编程技术网

javascript将日期转换为本地时区

javascript将日期转换为本地时区,javascript,Javascript,我有一个返回日期对象的函数。如何将其转换为用户的本地时区?用户可能在任何地方,因此不能硬编码 getNextDay = (dayIndex) => { const today = new Date(); today.setDate( today.getDate() + ((dayIndex - 1 - today.getDay() + 7) % 7) + 1 ); today.setHours(17, 30, 0); return today; }; 如果您

我有一个返回日期对象的函数。如何将其转换为用户的本地时区?用户可能在任何地方,因此不能硬编码

 getNextDay = (dayIndex) => {
  const today = new Date();
  today.setDate(
    today.getDate() + ((dayIndex - 1 - today.getDay() + 7) % 7) + 1
  );
  today.setHours(17, 30, 0);
  return today;
};

如果您的代码在浏览器中运行,则创建一个
新日期
将为您提供浏览器用户时区中的日期。@PatrickHund我正在设置小时数,因此需要将其转换为字符串,而不是时间?像今天一样。tolocalString()?hi@hitodo9051,可以使用vanilla js转换到不同的时区。但是为什么不使用像momentJS这样的第三方库来进行转换呢