I faced with the same problem:
6 October 2019 in Australia is Daylight Saving Time Starts.
dh.parse('2019-10-06')
returns: Sat Oct 05 2019 23:00:00 GMT+1000 (Australian Eastern Standard Time)
Solution is adding info about time zone (one of):
- Add sign of absent time zone offset “Z” -- dh.parse('2019-10-06T00:00:00.000Z' )
- Add GMT -- dh.parse('2019-10-06 GMT' )
- Add +00:00 -- dh.parse('2019-10-06T00:00:00.000+00:00' )
]1)
date-fns
date-fns.org
date-fns - modern JavaScript date utility library
import { format, formatDistance, formatRelative, subDays } from 'date-fns' format(new Date(), "'Today is a' eeee") //=> "Today is a Monday" formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true }) //=> "3 days ago" formatRelative(subDays(new Date(), 3), new Date()) //=> "last Friday at 7:26 p.m." With the function-per-file style, you can pick just what you need and stop bloating your project with useless functionality.
Top answer 1 of 2
3
I faced with the same problem:
6 October 2019 in Australia is Daylight Saving Time Starts.
dh.parse('2019-10-06')
returns: Sat Oct 05 2019 23:00:00 GMT+1000 (Australian Eastern Standard Time)
Solution is adding info about time zone (one of):
- Add sign of absent time zone offset “Z” -- dh.parse('2019-10-06T00:00:00.000Z' )
- Add GMT -- dh.parse('2019-10-06 GMT' )
- Add +00:00 -- dh.parse('2019-10-06T00:00:00.000+00:00' )
]1)
2 of 2
1
It is considering your time zone change and it is at the start of day (in your timezone, not UTC). Midnight in UTC is not necessarily start of day in your zone.
Check this out (I'm in GMT+1 zone):
const from = dateFns.parse('2019-03-31') // -> "2019-03-31T00:00:00.000+0100"
const fromPlusDay = dateFns.addDays(from, 1)
dateFns.format(fromPlusDay , 'YYYY-MM-DDTHH:mm:ss.SSSZZ') // -> "2019-04-01T00:00:00.000+0200"
I think what you are doing is fine, just don't expect your zone to be 00:00 in UTC, and avoid printing dates in UTC.
npm
npmjs.com › package › date-fns
date-fns - npm
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ]
» npm install date-fns
CodeSandbox
codesandbox.io › s › date-fns-adddays-ykxye
date-fns/addDays - CodeSandbox
Published Nov 16, 2021
Repository https://codesandbox.io/s/ykxye
DevDocs
devdocs.io › date_fns
DevDocs — date-fns documentation
date-fns 2.29.2 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
SitePoint
sitepoint.com › blog › javascript › managing dates and times in javascript using date-fns
Managing Dates and Times in JavaScript Using date-fns — SitePoint
November 13, 2024 - Also notice how the method name is more expressive (addDays instead of just add), keeping things consistent and having one method to do one thing and one thing only. If you look at the list of posts on SitePoint’s JavaScript channel, you can see that some are listed as being published on a certain date, whereas others are listed as being published X days ago. It might take a while if you tried to implement this in vanilla JavaScript, but with date-fns this is a breeze – just use the formatDistance method.
DigitalOcean
digitalocean.com › community › tutorials › js-date-fns
Quick Tour of date-fns, a Simple JavaScript Date Library | DigitalOcean
March 18, 2020 - For example, we can calculate the days from January 1st to Christmas (as well as “business days”!): const format = require('date-fns/format'); const addYears = require('date-fns/addYears'); const differenceInDays = require('date-fns/differenceInDays'); const differenceInBusinessDays = require('date-fns/differenceInBusinessDays') const startDate = new Date('2020/01/01'); const endDate = new Date('2020/12/24'); const daysBetween = differenceInDays(endDate, startDate); const workdaysBetween = differenceInBusinessDays(endDate, startDate); console.log(daysBetween); // => 358 console.log(workdaysBetween); // => 256 ·
Fulcrumapp
help.fulcrumapp.com › en › articles › 4037876-date-manipulation-with-date-fns
Date Manipulation with date-fns | Fulcrum Help Center
Working with JavaScript dates and times can quickly become challenging and date-fns provides over 140 functions for comparing, manipulating, and formatting dates and timestamps.
Medium
medium.com › @stheodorejohn › date-operations-in-javascript-with-date-fns-880bb182cec6
Date Operations in JavaScript with date-fns | by Theodore John.S | Medium
June 12, 2023 - Date manipulation is a common task in JavaScript development, and the date-fns library provides a powerful toolkit to simplify and enhance your date-related operations. In this article, we’ll explore ten activities you can perform with date-fns to supercharge your date manipulation capabilities.