Given the following 2 dates in ISO format: getDay() returns 0 for the first one, and 1 for the second one. Note that it’s the same date, and only time is changed. I am assuming it returns the result after converting it to local time. Because of that behavior, my logic has some side-effects. Is there a way to get
Tag: date-fns
UTC issue on formatting date
I have this react typescript/js code: but it’s not working properly. It changes fromDate to the next day when I run it late at night. So I tried on 6/30/2022 at 11pm and it changed it to 7/1/2022. My attempted fix is to use date-fns format function: My question is will this resolve the utc issue? Maybe I should rather
Date-fns Unexpected results when adding months
I am using date-fns for the first time in my project. I need to give user the ability to add a month to current date. I’m wondering how is this supposed to work with different length months? I tried different months with 30 and 31 days and I cannot make sense of it. If I add a month to July
How to correctly format date with timezone in date-fns?
I am using date-fns library but It is not formatting the date correctly. My code: Required result: Actual result: Please help me in finding the mistake. Thank you Answer To get your desired result using format…
Divide timespan in JavaScript
I have to recreate some logic from Kusto query language in JS and I’m struggling with duration division. How can I get quotient from two durations, like this : One can divide two timespan values to get their quotient. For example, 1d / 5h gives 4.8. This gives one the ability to express any timespan value as a multiple of
Sorting the array of objects by key name using date-fns library in javascript
Here array containing the name along with the updatedTimeStamp propery. I want to sort the array based on the updatedTimeStamp property. So I am hereby using date-fns library and I want to use this library only , I can do without this library but that’s my requirement to use this library. I can able to do sort based on the
chartjs time cartesian axis adapter and date library setup
I am trying to implement this tutorial and could not get it done. https://www.chartjs.org/docs/latest/axes/cartesian/time.html Input: list of objects with (time,value) attributes. Time is Integer that means unix time in seconds; value is Float. The tutorial says “Date Adapters. The time scale requires both a date library and a corresponding adapter to be present. Please choose from the available adapters”. Date
How to get the day name from the date string regardless of user’s timezone?
Given a date (without time) e.g. 2021-08-11, how could I get its day name (e.g. Wednesday) regardless of the user’s timezone? Note that trying to use: or date-fns’s format: results in the wrong day (Tuesday) if the user’s timezone is GMT-7, for example. Answer You can use date-fns-tz as suggested by the other comment. It interally uses Intl. If you
How to use date-fns in a Node.js project?
I installed date-fns as per the following instruction on https://www.npmjs.com/package/date-fns After that my package.json is updated with the following entry: Then, I wrote the following code from https://date-fns.org/ and it resulted in error: Error: node -v: Answer I managed to run it successfully by using require as shown below: Update: I installed node v16.6.1 following the instructions in this answer
Date-fns is returning incorrect dates
I have a collection of date strings stored in a database in this format: 2018-06-28T14:06:26.524Z 2018-07-02T10:32:18.818Z 2018-07-06T15:08:50.233Z I need to convert these dates into a format like this on the frontend: 28 June 2018 14:06:26 02 July 2018 10:32:18 06 July 2018 08:50:23 My attempt at doing this using date-fns is: The problem is that the dates returned from the