Skip to content

Commit

Permalink
Added .format method to LocalDate, LocalDateTime and ZonedDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidDuwaer committed May 26, 2021
1 parent 3a36616 commit 966e216
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/LocalDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ export class LocalDate
);
}

/**
* Writes this {@link ZonedDateTime} as a string in specified format, and returns it.
* @param formatString The format in which to write the date. The following substrings
* (expressed in <b>bold</b>) will get translated to the the following values:
* <ul>
* <li><b>YY</b> - year, short - 21</li>
* <li><b>YYYY</b> - year, full - 2021</li>
* <li><b>M</b> - month - 1..12 (January is 1)</li>
* <li><b>Mo</b> - month - 1st..12th (January is 1st)</li>
* <li><b>MM</b> - month - 01..12 (January is 01)</li>
* <li><b>MMM</b> - month, short - Jan</li>
* <li><b>MMMM</b> - month, full - January</li>
* <li><b>Q</b> - quarter - 1..4 (Jan-March is 1)</li>
* <li><b>Qo</b> - quarter - 1st..4th (Jan-March is 1st)</li>
* <li><b>d</b> - day of week - 0..6 (Monday is )</li>
* <li><b>dd</b> - day of week, 2 characters - Mo, Su</li>
* <li><b>ddd</b> - day of week, 3 characters - Mon, Sun</li>
* <li><b>dddd</b> - day of week, full - Monday, Wednesday</li>
* <li><b>D</b> - day of month - 1..31</li>
* <li><b>Do</b> - day of month - 1st..31st</li>
* <li><b>DD</b> - day of month - 01..31</li>
* <li><b>DDD</b> - day of year - 1..365 (1..366 for leap years)</li>
* <li><b>DDDo</b> - day of year - 1st..365th (1st..366th for leap years)</li>
* <li><b>DDDD</b> - day of year - 001..365 (001..366 for leap years)</li>
* <li><b>w</b> - week of year - 1..53</li>
* <li><b>wo</b> - week of year - 1st..53rd</li>
* <li><b>ww</b> - week of year - 01..53</li>
* <li>And more. Formatting currently provided by moment.js, so for a complete list, see
* <a href="https://devhints.io/moment#formatting-1">the moment.js cheatsheet</a>.</li>
* </ul>
*/
public format(formatString: string): string
{
return this.toMoment().format(formatString);
}

/**
* To ISO-8601 string, e.g. "2020-01-23"
*/
Expand Down
36 changes: 36 additions & 0 deletions src/LocalDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ export class LocalDateTime
return this.time.millisecond;
}

/**
* Writes this {@link ZonedDateTime} as a string in specified format, and returns it.
* @param formatString The format in which to write the date. The following substrings
* (expressed in <b>bold</b>) will get translated to the the following values:
* <ul>
* <li><b>YY</b> - year, short - 21</li>
* <li><b>YYYY</b> - year, full - 2021</li>
* <li><b>M</b> - month - 1..12 (January is 1)</li>
* <li><b>Mo</b> - month - 1st..12th (January is 1st)</li>
* <li><b>MM</b> - month - 01..12 (January is 01)</li>
* <li><b>MMM</b> - month, short - Jan</li>
* <li><b>MMMM</b> - month, full - January</li>
* <li><b>Q</b> - quarter - 1..4 (Jan-March is 1)</li>
* <li><b>Qo</b> - quarter - 1st..4th (Jan-March is 1st)</li>
* <li><b>d</b> - day of week - 0..6 (Monday is )</li>
* <li><b>dd</b> - day of week, 2 characters - Mo, Su</li>
* <li><b>ddd</b> - day of week, 3 characters - Mon, Sun</li>
* <li><b>dddd</b> - day of week, full - Monday, Wednesday</li>
* <li><b>D</b> - day of month - 1..31</li>
* <li><b>Do</b> - day of month - 1st..31st</li>
* <li><b>DD</b> - day of month - 01..31</li>
* <li><b>DDD</b> - day of year - 1..365 (1..366 for leap years)</li>
* <li><b>DDDo</b> - day of year - 1st..365th (1st..366th for leap years)</li>
* <li><b>DDDD</b> - day of year - 001..365 (001..366 for leap years)</li>
* <li><b>w</b> - week of year - 1..53</li>
* <li><b>wo</b> - week of year - 1st..53rd</li>
* <li><b>ww</b> - week of year - 01..53</li>
* <li>And more. Formatting currently provided by moment.js, so for a complete list, see
* <a href="https://devhints.io/moment#formatting-1">the moment.js cheatsheet</a>.</li>
* </ul>
*/
public format(formatString: string): string
{
return this.toMoment().format(formatString);
}

/**
* To ISO-8601 string, e.g. "2020-01-23T17:34:00.000"
*/
Expand Down
36 changes: 36 additions & 0 deletions src/ZonedDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,42 @@ export class ZonedDateTime
return this.zonedMoment.millisecond();
}

/**
* Writes this {@link ZonedDateTime} as a string in specified format, and returns it.
* @param formatString The format in which to write the date. The following substrings
* (expressed in <b>bold</b>) will get translated to the the following values:
* <ul>
* <li><b>YY</b> - year, short - 21</li>
* <li><b>YYYY</b> - year, full - 2021</li>
* <li><b>M</b> - month - 1..12 (January is 1)</li>
* <li><b>Mo</b> - month - 1st..12th (January is 1st)</li>
* <li><b>MM</b> - month - 01..12 (January is 01)</li>
* <li><b>MMM</b> - month, short - Jan</li>
* <li><b>MMMM</b> - month, full - January</li>
* <li><b>Q</b> - quarter - 1..4 (Jan-March is 1)</li>
* <li><b>Qo</b> - quarter - 1st..4th (Jan-March is 1st)</li>
* <li><b>d</b> - day of week - 0..6 (Monday is )</li>
* <li><b>dd</b> - day of week, 2 characters - Mo, Su</li>
* <li><b>ddd</b> - day of week, 3 characters - Mon, Sun</li>
* <li><b>dddd</b> - day of week, full - Monday, Wednesday</li>
* <li><b>D</b> - day of month - 1..31</li>
* <li><b>Do</b> - day of month - 1st..31st</li>
* <li><b>DD</b> - day of month - 01..31</li>
* <li><b>DDD</b> - day of year - 1..365 (1..366 for leap years)</li>
* <li><b>DDDo</b> - day of year - 1st..365th (1st..366th for leap years)</li>
* <li><b>DDDD</b> - day of year - 001..365 (001..366 for leap years)</li>
* <li><b>w</b> - week of year - 1..53</li>
* <li><b>wo</b> - week of year - 1st..53rd</li>
* <li><b>ww</b> - week of year - 01..53</li>
* <li>And more. Formatting currently provided by moment.js, so for a complete list, see
* <a href="https://devhints.io/moment#formatting-1">the moment.js cheatsheet</a>.</li>
* </ul>
*/
public format(formatString: string): string
{
return this.zonedMoment.format(formatString);
}

/**
* To ISO-8601 string, e.g. "2020-01-23T17:34:00.000Z"
*/
Expand Down

0 comments on commit 966e216

Please sign in to comment.