Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Javascript Date

Javascript Date

Avatar for yujing.wang

yujing.wang

June 20, 2018

More Decks by yujing.wang

Other Decks in Technology

Transcript

  1. 问题 • new Date(0, 0, 0); //chrome? Safari? Why? •

    Cookie 的expires怎么设置? • 夏令时(DST) • Time Zone offset • 怎么知道今年2月份有多少天? • UTC和GMT啥关系?
  2. 构造函数 • new Date(); • new Date(value); • new Date(dateString);

    • new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
  3. 构造函数 • new Date(); • new Date(value); //从1970年1月1日00:00:00(UTC)起经过的毫秒数:new Date(0) •

    new Date(dateString); //接收ISO8601(YYYY-MM-DDTXX:XX:XX[Z|| [+|-]XX:XX] 或者 IETF RFC 2822 (Jun 1, 1995 03:24:00) [new Date(‘Jan 1, 1970 00:00:00’) / new Date(‘Jan 1, 1970 00:00:00Z’) / new Date(‘Jan 1, 1970 00:00:00+08:00’) / / new Date(‘Jan 1, 1970 00:00:00+0800’)] -à chrome? Safari? 区别: 1970-1-1、1970/1/1、1970-01-01、1970/01/01… • new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]); //date如果是<=0, 日期向之前计算,如果是>0 日期向未来计算 参考: https://tools.ietf.org/html/rfc2822#page-14
  4. 取值范围 • year代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要 用 98. • month代表月份的整数值从0(1月)到11(12月)。

    (0-11) • day代表一个月中的第几天的整数值,从1开始。 (1-31) • hour代表一天中的小时数的整数值 (24小时制)。 (0-23) • minute分钟数。(0,59) • second秒数。(0,59) • millisecond表示时间的毫秒部分的整数值。(0,999)
  5. 实例方法 • Balabala… • getDay 星期几? 周日是0 • getFullYear取代getYear •

    toString toDateString… • toUTCString vs toGMTString • setXX取值和构造函数相同
  6. 时区、GMT/UTC、DST和getTimezoneOffset • Z的由来: Zulu ,正好和 GMT相同 • DST: daylight saving

    time (PST,PDT,CST,CDT…) • 时钟在三月向前调一小时,在11月向后调一小时(每年都不相同) • 俄罗斯实行永久夏令时 • 我们和米国的时差缩小了一小时 • getTimezoneOffset和DST相关 • 不可靠,得到的是和DST相关的时差,需要计算 • 比如我们和洛杉矶正常时差16小时,现在是15小时 • IANA数据库
  7. toLocaleString & Intl.DateTimeFormat • toLocaleString: dateObj.toLocaleString([locales][, options]) • Intl.DateTimeFormat: new

    Intl.DateTimeFormat([locales][, options]) var date = new Date; var options = { weekday: "long", year: "numeric", month: "short", day: "numeric" , hour: 'numeric',timeZone: 'America/New_York'}; var d =\new Intl.DateTimeFormat('chinese', options).format(date); 格式参考: https://tools.ietf.org/html/rfc5646
  8. 问题 • new Date(0, 0, 0); //chrome? Safari? Why? •

    Cookie 的expires怎么设置? //GMT • 夏令时(DST) //中国为什么不实施了 • Time Zone offset • 怎么知道今年2月份有多少天? • UTC和GMT啥关系?
  9. 参考 • https://docs.microsoft.com/zh-cn/scripting/javascript/reference/date-object-javascript • https://docs.microsoft.com/zh-cn/scripting/javascript/date-and-time-strings-javascript#bkmk_extend • https://docs.microsoft.com/zh-cn/scripting/javascript/reference/intl-datetimeformat-object-javascript • https://stackoverflow.com/tags/timezone/info •

    https://stackoverflow.com/tags/dst/info • https://24timezones.com/map_zh.php#/map • https://stackoverflow.com/questions/11887934/how-to-check-if-the-dst-daylight-saving-time-is-in-effect-and- if-it-is-whats • https://docs.microsoft.com/zh-cn/scripting/javascript/reference/intl-datetimeformat-object-javascript • https://moment.github.io/luxon/ • https://github.com/iamkun/dayjs/