【问题标题】:Get the local date instead of UTC获取本地日期而不是 UTC
【发布时间】:2020-09-27 06:33:18
【问题描述】:

以下脚本计算我下周五和下周日的日期。

问题:使用 .toISOString 使用 UTC 时间。我需要改变输出当地时间的东西。我对 javascript 很陌生,所以我找不到合适的属性来代替 .toIsostring。 我该怎么办?

function nextWeekdayDate(date, day_in_week) {
  var ret = new Date(date || new Date());
  ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
  return ret;
}

let nextFriday = nextWeekdayDate(null, 5);
let followingSunday = nextWeekdayDate(nextFriday, 0);

console.log('Next Friday     : ' + nextFriday.toDateString() +
  '\nFollowing Sunday: ' + followingSunday.toDateString());

/* Previous code calculates next friday and next sunday dates */


var checkinf = nextWeekdayDate(null, 5);
var [yyyy, mm, dd] = nextFriday.toISOString().split('T')[0].split('-');
var checkouts = nextWeekdayDate(null, 7);
var [cyyy, cm, cd] = followingSunday.toISOString().split('T')[0].split('-');

【问题讨论】:

  • 如果您还没有查看过Date.getTimezoneOffset 方法,您可能会发现它很有用。它“以分钟为单位返回从当前区域设置(主机系统设置)到 UTC 的时区差异。”您可以使用此信息在将日期对象转换为字符串之前(或将其保存到数据库等之前)相应地调整日期对象developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 与您的other question 一样,您的问题是how to format a date。在这种情况下,您可以使用nextFriday.toLocaleString('en-CA', {year:'numeric',month:'2-digit',day:'2-digit'})

标签: javascript date localdate localtime toisostring


【解决方案1】:

如果您担心某些时区的日期有误,请尝试将时间标准化

不使用 toISO 你可以这样做

const [dd1, mm1, yyyy1] = nextFriday.toLocaleString('en-GB', 
  { year: 'numeric', month: '2-digit', day: '2-digit' })
  .split("/")

function nextWeekdayDate(date, day_in_week) {
  var ret = new Date(date || new Date());
  ret.setHours(15, 0, 0, 0); // normalise
  ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
  return ret;
}

let nextFriday = nextWeekdayDate(null, 5);
let followingSunday = nextWeekdayDate(nextFriday, 0);

console.log('Next Friday     : ' + nextFriday.toDateString() +
  '\nFollowing Sunday: ' + followingSunday.toDateString());

/* Previous code calculates next friday and next sunday dates */


var checkinf = nextWeekdayDate(null, 5);
var [yyyy, mm, dd] = nextFriday.toISOString().split('T')[0].split('-');
var checkouts = nextWeekdayDate(null, 7);
var [cyyy, cm, cd] = followingSunday.toISOString().split('T')[0].split('-');

console.log(yyyy, mm, dd)

// not using UTC: 

const [dd1, mm1, yyyy1] = nextFriday.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).split("/")

console.log(yyyy1, mm1, dd1)

【讨论】:

  • 只是将时间标准化为 15:00,这应该可以正确处理所有时区
  • 除非用户在 -11:00 在 Pacific/Pago_Pago 或在 -10:00 在 Pacific/Tahiti。为什么不date.toLocaleString('en-CA', {year:'numeric',month:'2-digit',day:'2-digit'})
  • @RobG 我相信今天仍然是 15 - 11 点
  • 是的,但对于 -11,它是 15:00 加 11,所以明天 26:00 或 2:00 UTC。 :-) 你可以使用nextFriday.setUTCMinutes(nextFriday.getUTCMinutes() - nextFriday.getTimezoneOffset()) 然后nextFriday.toISOString().substring(0,10) 来做到这一点,但实际上这是一个格式问题,所以最好这样处理。
  • 好的,我改变了答案。谢谢
【解决方案2】:

您担心 [yyyy,mm,dd] 是 UTC 而不是当前时区?

nextFriday 是一个日期对象。如果您使用 get-functions 代替它会起作用吗? 例如

const nextFridayYear = nextFriday.getFullYear();
// get month is zero index based, i have added one
const nextFridayMonth = (nextFriday.getMonth() + 1).toString()
    .padStart(2, '0');
const nextFridayDay = today.getDate().toString()
    .padStart(2, '0');

【讨论】:

  • 可能的问题是当您在您的区域设置接近午夜时,您可能会得到错误的日期。我通过将时间设置为 15:00 解决了这个问题
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 2020-01-08
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2020-09-18
  • 2021-04-05
相关资源
最近更新 更多