1.0.1 • Published 1 month ago

dafo v1.0.1

Weekly downloads
65
License
MIT
Repository
github
Last release
1 month ago

dafo

General Date Format

total downloads of dafo dafo's License latest version of dafo build status of github.com/YounGoat/ecmascript.dafo

Description

To format date in PHP or MySQL style.

Table of Contents

DISCLAIMER

I understand that date and datetime is really important and sensitive. However, there are too many details to be dealt with in this package. So, there is NO guarantee that the outputs of dafo will conform to what described in this document.

If anything unexpected found, please create an issue to let me and others know.

Get Started

dafo offers different ways to format a date. And you may choose the style which you are familiar with.

MySQL Style

const DATE_FORMAT = require('dafo/mysql');

// Use customised format mask.
DATE_FORMAT(new Date, '%Y-%M-%d');
// e.g. RETURN 2018-1-1

// Use predefined format mask.
DATE_FORMAT(new Date, DATE_FORMAT.USA);

See MySQL Reference Manual Date and Time Functions, DATE_FORMAT for details, and GET_FORMAT for available predefined formats.

PHP Style

const date_format = require('dafo/php');

date_format(new Date, 'Y-n-j');
// e.g. RETURN 2018-1-1

See PHP Manual > Date/Time Functions for details.

API

dafo/mysql

const date_format = require('dafo/mysql');
  • string date_format(Date date, string format)
  • const string date_format.DATE.*
  • const string date_format.DATETIME.*
  • const string date_format.TIME.*

Special characters prefixed with % will be recognized and transformed into corresponding date text. Characters prefixed with % but unrecognizable will be preserved wite the leading % trimmed.

Follow the next table for frequently used format characters which supported by dafo/mysql:

CatergoryExampleChMeaning
DaySun..Sat%aweekday short name
Day1st,2nd..31st%Dday of the month (ordinal number)
Day01..31%dday of the month
Day1..31%eday of the month
Day001..366%jday of the year
DaySunday..Saturday%Wweekday name
Day0..6%wweekday number (0=Sunday..6=Saturday)
Week00..53%Uweek number of year (mode 0)
Week00..53%uweek number of year (mode 1)
Week01..53%Vweek number of year (mode 2)
Week01..53%vweek number of year (mode 3)
MonthJan..Dec%bmonth short name
Month1..12%cmonth number
MonthJanuary..December%Mmonth name
Month01..12%mmonth number
Year1999%Xweek-numbering year (mode 2)
Year1999%xweek-numbering year (mode 3)
Year2000%Yyear
Year00%yyear
Time000000..999999%fmicro-seconds
Time00..23%Hhours (24-hour)
Time01..12%hhours (12-hour)
Time01..12%Ihours (12-hour)
Time00..59%iminutes
Time0..23%khours (24-hour)
Time1..12%lhours (12-hour)
TimeAM, PM%pAnte meridiem / Post meridiem
Time11:59:59 AM%rtime (12-hour)
Time00..59%Sseconds
Time00..59%sseconds
Time23:59:59%Ttime (24-hour)

ATTENTION: Most, but NOT ALL format characters used in MySQL function DATE_FORMAT() are supported by dafo/mysql.

dafo/php

const date_format = require('dafo/php');
  • string date_format(Date date, string format)

As what date() in PHP does, dafo/php will recognize special single characters in format string and transform them into corresponding date text. Unlike dafo/mysql, there are no leading escapers before these special characters and those not recognized will be preserved.

Escape Character
If you want to keep the format character from being replaced, prefix it with an escape character anti-slash \, e.g.

date_format(new Date(2000, 0, 1), '\\Y Y');
// RETURN 'Y 2000'

// Because '\' is also the default escaper in JavaScript string, 
// you should input double anti-slash characters '\\' in string literal 
// to create a real '\' in the format.

Follow the next table for frequently used format characters which supported by dafo/php:

CatergoryExampleChMeaning
Day01..31dday of the month
DayMon..SunDweekday short name
Day1..31jday of the month
DaySunday..Saturdaylweekday name
Day1..7Nweekday number
Dayst,nd..stSday of the month (ordinal suffix)1
Day0..6wweekday number
Day0..365zday of the year
Week01..53Wweek number of year
MonthJanuary..DecemberFmonth name
Month01..12mmonth number
MonthJan..DecMmonth short name
Month1..12nmonth number
Month28..31tdays in the month
Year0,1Lif it is a leap year
Year1999, 2000oweek-numbering year
Year0100, 1899, 2050Yyear A.D.
Year00, 99, 50yyear A.D.
Timeam, pmaAnte meridiem / Post meridiem
TimeAM, PMAAnte meridiem / Post meridiem
Time1..12g12 hour
Time0..23G24 hour
Time01..12h12 hour
Time00..23H24 hour
Time00..59iminutes
Time00..59sseconds
Time000000..999999umicro-seconds
Time000...999vmilli-seconds
TimeZone+0800Ooffset from GMT (in hours and minutes)
TimeZone+08:00Poffset from GMT (in hours and minutes)
TimeZone28800Zoffset from GMT (in seconds)

ATTENTION: Most, but NOT ALL format characters used in PHP function date() are supported by dafo/php.

dafo.parse

const dafo = require('dafo');

// Get a Date object.
let date = dafo.parse('2019.04.01', 'Y.m.d');
  • Date dafo.parse(string date, string format)

Argument format describes what and how makes up date string. Acceptable placeholders include:
( Other characters are regarded as normal text. )

CatergoryExampleChMeaning
Day01..31dday of the month
Month01..12mmonth number
Year0100, 1899, 2050Yyear A.D.
Time00..23H24 hour
Time00..59iminutes
Time00..59sseconds

dafo.toSeconds

const dafo = require('dafo');
let secondCount = dafo.toSeconds('1d2h');
  • Date dafo.toSeconds(string age)

Argument age is an expression used to describe age, generally that of HTTP cache.

UnitMeaning
ssecond(s)
mminute(s)
hhour(s)
dday(s)
Mmonth(es)
Yyear(s)

Others

  • number dafo.getDayOfYear(Date date)
    Get the day of the current year.
    Jan 1st is always 1, and Dec 31st may be 365 (not leap year) or 366 (leap year).

  • number dafo.getMonthDays(Date date)
    Get days of the current month.

  • number dafo.getWeekOfYear(Date date, number mode)
    Get the week number of year.
    Read section Week Mode for details about the mode argument.

  • { Date first, Date last } dafo.getWeekRange({ number year, number week, number mode })
    Get the week range.
    Read section Week Mode for details about the mode argument.

  • { year, week } dafo.getYearWeek(Date date, number mode)
    Get the week number of year, and the corresponding year.
    In some mode, the first few days in a year may be belonging to the last week of last year.
    Read section Week Mode for details about the mode argument.

  • boolean dafo.isLeapYear(Date date)
    Return true if the year of date is leap. Otherwise return false.

  • Date dafo.parse(string date, string format)

Week Mode

The definition of mode argument in functions dafo.getWeekOfYear() and dafo.getYearWeek() is borrowed from MySQL function WEEK(). The next table is cited from MySQL Manual and explains the difference between modes:

Mode1st weekdayRangeWeek 1 is the first week ...Follows
0Sunday0-53with a Sunday in this year
1Monday0-53with 4 or more days this year
2Sunday1-53with a Sunday in this year
3Monday1-53with 4 or more days this yearISO 8601
4Sunday0-53with 4 or more days this year
5Monday0-53with a Monday in this year
6Sunday1-53with 4 or more days this year
7Monday1-53with a Monday in this year

Why dafo

There have been lots of packages helping to format date or datetime. Generally, a format string is required to indicate what kind of date string you want, e.g. YY in moment means 2 digit year. The formats used in those packages are similiar, but more or less different from each other. It is really confusing! For those who are familiar with syntax used in date() in PHP or DATE_FORMAT() in MySQL, it is wasting time to learn another one.

I am tired of inventing something new but useless. What uou will not meet with in dafo is NOT a new general format, but something you have been familiar with.

About

References