Date In JavaScript/TS

这篇博客探讨了在JavaScript中处理日期时间时遇到的国际化问题,包括时区影响、日期格式不一致以及浏览器差异。文章强调了UTC时间、ISO格式和本地时间的使用,并指出在创建和获取日期时未指定时区可能导致的问题。还提到了某些浏览器对非标准日期格式的处理不一致,可能导致错误。解决方案包括使用统一的日期时间格式和理解JavaScript在不同浏览器中的行为。
摘要由CSDN通过智能技术生成

Why we need to konw date?

  • We need to handle date time-zone influence during international project.
  • We need to konw date format showing in client.

How can we learn it?

  1. Jave Script Document
  2. ES5/6 Document
  3. Test in browsers

What is the solution/result?

  1. UTC (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time).
  2. Omitting T or Z in a date-time string can give different results in different browsers.
  3. (TZ)When setting a date, without specifying the time zone, JavaScript will use the browser’s time zone.
  4. (TZ)When getting a date, without specifying the time zone, the result is converted to the browser’s time zone.
  5. (TZ)In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to CDT (Central US Daylight Time) if a user browses from central US.
  6. In some browsers, months or days with no leading zeroes may produce an error:
const d = new Date("2015-3-25");
  1. The behavior of “YYYY/MM/DD” is undefined.
    Some browsers will try to guess the format. Some will return NaN.
const d = new Date("2015/03/25")
  1. The behavior of “DD-MM-YYYY” is also undefined.
    Some browsers will try to guess the format. Some will return NaN.
const d = new Date("25-03-2015");
  1. UTC
const d = (new Date()).toUTCString(); // 'Wed, 09 Mar 2022 07:18:02 GMT'
  1. ISO
const d = (new Date()).toISOString(); // '2022-03-09T07:18:59.078Z'
  1. Local
const d = (new Date()).toLocaleString(); // '2022/3/9 13:58:15'

https://www.w3schools.com/js/js_date_formats.asp
https://www.w3schools.com/js/js_es6.asp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值