Skip to content

Getting Start with Date

Advantages

  • Better solution for Function Encapsulation
  • Enhance your code Maintainability
  • Connect with other modules easily
  • Modular Design

Import Module

typescript
import { date } from "jsfast";

Getting a time string

A lot of projects need to get a time string from a specific format string, but this is not easy to do with built-in Date object.

But with FastjsDate, you can do it easily.

Format string

If you don't pass a format string, it will default to Y-M-D h:m:s.

For more information about date format, scroll down to see the format string table.

typescript
date.string("Y-M-D h:m:s"); // Get the current time string
date.string("Y-M-D h:m:s", 1666351246); // Convert timestamp to string

As you can easy, it is super easy to use, we provide a clearly user-friendly API name and a simple way to use it, not only in this module but also in all modules.

Type Declaration

typescript
declare const date: {
  string: (format: string, date?: number) => string;
  date: (format: string, date: string) => Date;
  reformat: (format: string, date: string, newFormat?: string) => string;
};
export default date;

Parsing a date string

Some date picker libraries may return a string of date, but you need to convert it to a timestamp.

We provide a function to parse a date string to a timestamp easily.

typescript
date.parse(1666351246);

Using parse function

Format string

If you don't pass a format string, it will default to Y-M-D h:m:s.

For more information about date format, scroll down to see the format string table.

Misunderstanding

The param format that you pass to parse function is the format of the string you pass to the function, not what you want to return.

Function parse can parse a string or a timestamp to a parseReturn object.

There are some functions that help you to call parse easily, they all return a parseReturn object.

typescript
date.parse("2022-10-21 19:20:46", "Y-M-D h:m:s");
date.parse(1666351246, "Y-M-D h:m:s");
date.parseDate("2022-10-21 19:20:46", "Y-M-D h:m:s");
date.parseTime(1666351246, "Y-M-D h:m:s");
date.now("Y-M-D h:m:s");

Return Object

These all return an parseReturn object, and this object contains a lot of information.

KeyDescriptionExample
formatformat stringY-M-D h:m:s
dateDate object2021-10-21T11:20:46.000Z
stringstring of Date2021-10-21 19:20:46
dateStringstring of Date, same as string2021-10-21 19:20:46
timestamptimestamp of Date1666351246
utcDateDate object of UTC2021-10-21T11:20:46.000Z
utcTimestamptimestamp of UTC1666351246
utcDateStringstring of UTC2021-10-21 19:20:46

Type Declaration

typescript
declare const _default: {
  parse: (time: string | number, format?: string) => parseReturn;
  parseDate: (date: string, format?: string | undefined) => parseReturn;
  parseTime: (time: number, format?: string | undefined) => parseReturn;
  now: (format?: string | undefined) => parseReturn;
};
typescript
export interface parseReturn {
  /** @description format string */
  format: string;
  /** @description Date object */
  date: Date;
  /** @description string of Date */
  string: string;
  /** @description string of Date, same as string */
  dateString: string;
  /** @description timestamp of Date */
  timestamp: number;
  /** @description Date object of UTC */
  utcDate: Date;
  /** @description timestamp of UTC */
  utcTimestamp: number;
  /** @description string of UTC */
  utcDateString: string;
}

Reformatting a date string

Format string

This is a simplified version of the parse function, it only returns a string of the date.

Type Declaration

typescript
declare const _default: {
  reformat: (format: string, date: string, newFormat?: string) => string;
};

Format Table

FormatDescriptionExample
YYear2021
MMonth01
DDay01
hHour01
hhHour without prefix 01
mMinute01
mmMinute without prefix 01
sSecond01
ssSecond without prefix 01
SMillisecond613
HHour(12h)01
AAM/PMAM
aam/pmam