项目作者: Uzumachi02

项目描述 :
Simple helper for Deno to convert Date format to string. Possibility to use predefined or custom formats.
高级语言: TypeScript
项目地址: git://github.com/Uzumachi02/date-format-deno.git
创建时间: 2020-07-30T13:55:31Z
项目社区:https://github.com/Uzumachi02/date-format-deno

开源协议:MIT License

下载


date-format-deno

date-format-deno ci

Simple helper for Deno to convert Date format to string. Possibility to use predefined or custom formats.

Usage

dateToString

Format Date to string

  1. import { dateToString } from "https://deno.land/x/date_format_deno/mod.ts"
  2. // use current date
  3. dateToString("dd-MM-yyyy"); // return : "06-07-2020"
  4. dateToString("hh:mm:ss"); // return : "12-11-10"
  5. ...
  6. const myDate = new Date("2020-12-11T10:09:54.321");
  7. dateToString("yyyy-MM-dd hh:mm:ss", myDate); // return : "2020-12-11 10:09:54"
  8. dateToString("hh:mm:ss.SSS", myDate); // return : "10:09:54.321"
  9. ...
  10. // all formats
  11. dateToString("dd-MM-y-yy-yyy-yyyy hh:mm:ss:SSS O P", new Date("2020-12-11T10:09:54.321"));
  12. // return : "11-12-20-20-20-2020 10:09:54:321 +0300 +03:00"
  13. ...
  14. // use predefined format
  15. const date = new Date("2020-12-11T10:09:54.321");
  16. dateToString("ISO8601", date); // return : "2020-12-11T10:09:54.321"
  17. dateToString("ISO8601_WITH_TZ_OFFSET", date); // return : "2020-12-11T10:09:54.321+0300"
  18. dateToString("DATE", date); // return : "2020-12-11"
  19. dateToString("TIME", date); // return : "10:09:54"
  20. dateToString("DATETIME", date); // return : "2020-12-11 10:09:54"
  21. dateToString("ABSOLUTETIME", date); // return : "10:09:54.321"
  22. // use default format
  23. dateToString(date); // return : "2020-12-11 10:09:54.321"
  24. ...

registerFormat

Register new format or redefine standard formats

  1. import {
  2. dateToString,
  3. registerFormat,
  4. } from "https://deno.land/x/date_format_deno/mod.ts";
  5. // new format
  6. registerFormat({ myFormat: "yyyy/MM/dd hh-mm-ss" });
  7. ...
  8. dateToString("MYFORMAT"); // return : "2020/07/06 05-04-03"
  9. ...
  10. // redefine format
  11. registerFormat({ default: "hh:mm:ss dd.MM.yyyy", time: "hh:mm" });
  12. ...
  13. const date = new Date("2020-12-11T10:09:54.321");
  14. dateToString(date); // return : "10:09:54 11.12.2020"
  15. dateToString("TIME", date); // return : "09:54"
  16. ...

utcDate

Gets a Date object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

  1. import {
  2. dateToString,
  3. utcDate,
  4. } from "https://deno.land/x/date_format_deno/mod.ts";
  5. const nowDate = new Date();
  6. const utcNowDate = utcDate();
  7. dateToString(nowDate); // return : "2020-07-30 16:19:14.395"
  8. dateToString(utcNowDate); // return : "2020-07-30 13:19:14.395"
  9. ...
  10. const tmpDate = new Date("2020-12-11T10:09:54.321");
  11. const tmpUtcDate = utcDate(tmpDate);
  12. dateToString(nowDate); // return : "2020-12-11 10:09:54.321"
  13. dateToString(utcNowDate); // return : "2020-12-11 07:09:54.321"
  14. ...

getGMTOffset

Convert timezoneOffset number to GMT string

  1. import { getGMTOffset } from "https://deno.land/x/date_format_deno/mod.ts";
  2. const nowDate = new Date();
  3. getGMTOffset(nowDate.getTimezoneOffset()); // return : "+0300"
  4. // use delimiter
  5. getGMTOffset(nowDate.getTimezoneOffset(), true); // return : "+03:00"
  6. ...
  7. let offset = 345;
  8. getGMTOffset(offset); // return : "-0545"
  9. getGMTOffset(offset, true); // return : "-05:45"
  10. ...
  11. offset = -570;
  12. getGMTOffset(offset); // return : "+0930"
  13. getGMTOffset(offset, true); // return : "+09:30"
  14. ...

Format keys

Key Description Example
dd Day of the month, 2 digits with leading zeros 01 to 31
MM Numeric representation of a month, with leading zeros 01 to 12
yy A two digit representation of a year 99 or 09
yyyy A full numeric representation of a year, 4 digits 1999 or 2020
hh 24-hour format of an hour with leading zeros 00 to 23
mm Minutes with leading zeros 00 to 59
ss Seconds with leading zeros 00 to 59
SSS Milliseconds, 3 digits with leading zeros 001 to 999
O Difference to Greenwich time (GMT) without colon between hours and minutes +0200
P Difference to Greenwich time (GMT) with colon between hours and minutes +02:00

Predefined formats

Key Format Example return
DEFAULT “yyyy-MM-dd hh:mm:ss.SSS” 2020-12-11 10:09:54.321
ISO8601 “yyyy-MM-ddThh:mm:ss.SSS” 2020-12-11T10:09:54.321
ISO8601_WITH_TZ_OFFSET “yyyy-MM-ddThh:mm:ss.SSSO” 2020-12-11T10:09:54.321+0300
DATE “yyyy-MM-dd” 2020-12-11
TIME “hh:mm:ss” 10:09:54
DATETIME “yyyy-MM-dd hh:mm:ss” 2020-12-11 10:09:54
ABSOLUTETIME “hh:mm:ss.SSS” 10:09:54.321