Package Exports
- date-as-string
- date-as-string/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (date-as-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Date as String
date-as-string returns the passed-in date object as a string in the format YYYY-MM-DD, irrespective of timezone.
This differs from formatting the date with .toISOString().split('T')[0], which adjusts the date to UTC, risking the returned string differing from that of the passed-in date.
For example, if I run new Date("2024-05-08T00:00:00.000-08:00").toISOString().split('T')[0] it returns '2024-05-08', which is what I want.
However, if I instead run new Date("2024-05-08T00:00:00.000+08:00").toISOString().split('T')[0] (note the different timezone), it returns '2024-05-07' because when converted to UTC, this date is 4pm on the 7th.
Using date-as-string fixes this problem, returning '2024-05-08' for both of these dates.
Setup
Install the package:
npm install date-as-stringUsage
date-as-string accepts one argument.
- Only date objects are accepted.
- If no argument is passed-in, it defaults to
new Date().
Examples
import dateAsString from 'date-as-string';
// Assuming today is 8th May 2024
console.log(dateAsString());
// 2024-05-08
console.log(dateAsString(new Date('2024-01-01T00:00:00.000-08:00')));
// 2024-01-01
console.log(dateAsString(new Date('2024-01-01T00:00:00.000+08:00')));
// 2024-01-01
console.log(dateAsString(new Date('2024-01-01')));
// 2024-01-01
console.log(dateAsString('2024-01-01'));
// Error: The argument passed to date-as-string must be a date object