如何使用date-fns格式化日期字符串而不进行时区转换?

3
我有一个简单的日期字符串,格式为ISO:const mytDate = '2021-10-24T16:00:00.000Z'。我想将其格式化为format(new Date(myDate), 'yyyy-MM-dd HH:mm:ss'),但是new Date()会将其转换为本地时区,而我不想要这样。我希望能够直接按照原来的字符串格式进行格式化。
是否有解决方案?
谢谢!
1个回答

0

我没有任何解决方案,所以我准备了自定义工具:

const getRawDate = (rawDate) => {
  if (!isValid(new Date(rawDate))) {
    return null;
  }

  // Split by T separator in order to take date and time
  const [date, time] = rawDate.split('T');
  // return date and time without timezone indication.
  return `${date} ${time.slice(0, -5)}`;
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接