1.7.1 • Published 4 years ago

g2dateformat v1.7.1

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

g2DateFormat

This library attempts to provide an easier way to retrieve datetime data by manipulating JS date and time.

This library is easy to use and highly customisable!

Not only can this library help you to acquire current datatime, it can also retrieve specific datetime with timezone offset!

Getting Started

Install

npm install g2dateformat

Then...

import g2DateFormat from 'g2DateFormat';

console.log(g2DateFormat("full_date_weekday", '', 0, 0));

Expected outcome:

1 September 2020, Sunday

Usage

g2DateFormat(output_format, seperator, input_datetime_value, timezone_offset);

This function has 4 parameters:

  • output format
  • seperator
  • input datetime value (default=0)
  • timezone offset (default=0)

The first and the second parameter have no default value, the function will return false if any of them is left blank.

Example

let current_date = g2DateFormat("YYYYMMDD", ".", 0, 0);
let current_time = g2DateFormat("HHMMSS", ":", 0, 0);
console.log(`Current Date: ${current_date}`);
console.log(`Current Time: ${current_time}`);

Expected output:

Current Date: 2020.09.25
Current Time: 12:30:33

Data Type of the returned value

Return value:

String

Output Formats

There are 40 different output formats:

Basic Formats

formatOutput
timestamp1598936829
full_date1 September 2020
full_date_DD01 September 2020
full_date_weekday1 September 2020, Sunday
full_date_DD_weekday01 September 2020, Sunday
month_yearSeptember 2020
weekdaySunday
YYYY2020
M9
MM09
D1
DD01

Date

formatOutput
YYYYMM202009
YYYYMMDD20200901
MMDD0901
MMYYYY092020
DDMMYYYY01092020
DDMM0109
YYYYM20209
YYYYMD202091
MD91
MYYYY92020
DMYYYY192020
DM19

Time

formatOutput
H5
M7
S9
HH05
MM07
SS09
HM57
HMS579
HHMM0507
HHMMSS050709
H_APM5am
APMam

Time with Sperator

formatOutput
HH:MM05:07
HH:MM:SS05:07:09
H:MM:SS_APM5:07:09am
H:MM_APM5:07am

The seperator parameter has no effect to the output. An empty string "" is expected when you wish to return any of the above output format.

That means no matter what you pass into the seperator parameter, the output will always be the format stipulated by the output_format parameter.

Example

Recommended:

let example = g2DateFormat("HH:MM:SS", "", 0, 0);
console.log(example);

Expected output:

05:07:09

Another example:

let example = g2DateFormat("HH:MM:SS", "-", 0, 0);
console.log(example);

Expected output:

05:07:09

Input datetime Value

It is the same as creating a new Date object with the new Date() constructor.

There are 4 ways to pass this argument:

0 // default value, which will return the current datetime
year, month, day, hours, minutes, seconds, milliseconds
milliseconds
date string

Example:

g2DateFormat("YYYYMMDD", "-", 0, 0); // 2020-09-01
g2DateFormat("HHMMSS", ":", "2015-01-01 12:05:35", 0); // 12:05:35
g2DateFormat("YYYYMMDD", "-", 1598936829, 0); // 2020-09-01
g2DateFormat("YYYYMMDD", ".", "2000-05-30", 0); // 2000.05.30

Timezone Offset

If you wish to convert the datetime to a specific timezone, you can simply pass the timezone offset (in UNIX format) as the 4th argument.

This could be left blank and the return value will be the datetime data in your local timezone setting.

Example

Original Time (GMT+08:00, Timezone Offset in Seconds: +28800):

2020-01-01 11:00:00

London Time (GMT+01:00, Timezone Offset in Seconds: +3600);

g2DateFormat("YYYYMMDD", "-", 1577847600, 3600);
// Output: 2020-01-01

g2DateFormat("HHMMSS", ":", 1577847600, 3600);
// Output: 04:00:00

New York Time (GMT-04:00, Timezone Offset in Seconds: -14400);

g2DateFormat("YYYYMMDD", "-", 1577847600, -14400);
// Output: 2019-12-31

g2DateFormat("HHMMSS", ":", 1577847600, -14400);
// Output: 23:00:00
1.7.1

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.2.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago