写个标准化的日期格式化库

创建计划
2018-01-04 15:48:31
进行中
耗时 15时27分9秒
关闭计划
2018-01-05 07:15:40
已完成

初衷/目标

因为日期格式并不统一,在写Go之后,发现Js的日期格式化并不方便,并不规范。

哪个有moment,可以高度自定义日期格式, 但是依旧不是规范的日期。

  now := time.Now()

  fmt.Println(now.Format(time.ANSIC))       // Thu Jan  4 23:42:52 2018
  fmt.Println(now.Format(time.UnixDate))    // Thu Jan  4 23:42:52 CST 2018
  fmt.Println(now.Format(time.RubyDate))    // Thu Jan 04 23:42:52 +0800 2018
  fmt.Println(now.Format(time.RFC822))      // 04 Jan 18 23:42 CST
  fmt.Println(now.Format(time.RFC822Z))     // 04 Jan 18 23:42 +0800
  fmt.Println(now.Format(time.RFC850))      // Thursday, 04-Jan-18 23:42:52 CST
  fmt.Println(now.Format(time.RFC1123))     // Thu, 04 Jan 2018 23:42:52 CST
  fmt.Println(now.Format(time.RFC1123Z))    // Thu, 04 Jan 2018 23:42:52 +0800
  fmt.Println(now.Format(time.RFC3339))     // 2018-01-04T23:42:52+08:00
  fmt.Println(now.Format(time.RFC3339Nano)) // 2018-01-04T23:42:52.983286+08:00
  fmt.Println(now.Format(time.Kitchen))     // 11:42PM
  fmt.Println(now.Format(time.Stamp))       // Jan  4 23:42:52
  fmt.Println(now.Format(time.StampMilli))  // Jan  4 23:42:52.983
  fmt.Println(now.Format(time.StampMicro))  // Jan  4 23:42:52.983286
  fmt.Println(now.Format(time.StampNano))   // Jan  4 23:42:52.983286000
const moment = require('moment');

const defaultUtcOffset = moment().utcOffset();

const layer = {
  ANSIC: 'ddd MMM D  HH:mm:ss YYYY',
  UnixDate: 'ddd MMM  D HH:mm:ss z YYYY',
  RubyDate: 'ddd MMM D HH:mm:ss ZZ YYYY',
  RFC822: 'ddd MMM D HH:mm:ss ZZ YYYY z',
  RFC822Z: 'D MMM YY HH:mm ZZ',
  RFC850: 'dddd, DD-MMM-YY HH:mm:ss z',
  RFC1123: 'ddd, DD MMM YY HH:mm:ss z',
  RFC1123Z: 'ddd, DD MMM YY HH:mm:ss ZZ',
  RFC3339: 'YYYY-MM-DDTHH:mm:SSZ',
  RFC3339Nano: 'YYYY-MM-DDTHH:mm.SSSSSS:SSZ',
  Kitchen: 'h:mA',
  Stamp: 'MMM  D HH:mm:ss',
  StampMilli: 'MMM  D HH:mm:ss.SSS',
  StampMicro: 'MMM  D HH:mm:ss.SSSSSS',
  StampNano: 'MMM  D HH:mm:ss.SSSSSSSSS',
  ISO8601: 'YYYY-MM-DDTHH:mm:ss ZZ',
  ISO8601Z: 'YYYY-MM-DDTHH:mm:ss\\Z',
  ISODate: 'YYYY-MM-DD',
  ISOOrigin: 'YYYY-DDD',
  ISOTime: 'HH:mm:ss',
  ISOWeek: 'YYYY-\\Www',
  ISOWeekDay: 'YYYY-\\Www-d',
  SlashedDate: 'MM/DD/YYYY',
  StrfTimeClock: 'HH:mm',
  StrfTimeISODate: 'YYYY-MM-DD',
  StrfTimeKitchen: 'H:MM:SS A',
  StrfTimeShortdate: 'D-MMM-YYYY',
  UTC: 'ddd, DD MMM YYYY HH:mm:ss z'
};

for (let rfc in layer) {
  const tmp = layer[rfc];
  let utcOffset = tmp === layer.ISO8601Z ? 0 : defaultUtcOffset;
  console.log(
    moment()
      .utcOffset(utcOffset)
      .format(tmp)
  );
}
Fri Jan 5  11:22:14 2018
Fri Jan  5 11:22:14 UTC 2018
Fri Jan 5 11:22:14 +0800 2018
Fri Jan 5 11:22:14 +0800 2018 UTC
5 Jan 18 11:22 +0800
Friday, 05-Jan-18 11:22:14 UTC
Fri, 05 Jan 18 11:22:14 UTC
Fri, 05 Jan 18 11:22:14 +0800
2018-01-05T11:22:09+08:00
2018-01-05T11:22.097000:09+08:00
11:22AM
Jan  5 11:22:14
Jan  5 11:22:14.098
Jan  5 11:22:14.098000
Jan  5 11:22:14.098000000
2018-01-05T11:22:14 +0800
2018-01-05T03:22:14Z
2018-01-05
2018-5
11:22:14
2018-W01
2018-W01-5
01/05/2018
11:22
2018-01-05
11:01:09 AM
5-Jan-2018
Fri, 05 Jan 2018 11:22:14 UTC

TODO类型

  • 类库
  • 框架
  • 服务
  • 命令行工具

预计时间

  • 一天
  • 一周
  • 两周
  • 一月
  • 更久

相关链接

https://github.com/axetroy/RFCDate

大牛们的评论:朕有话说

还没有人评论哦,赶紧抢沙发!