My end goal is to check subscription.cancel_at_period_end for false and store subscription end date as a moment date object.
const subscriptionEndDate = moment(subscription.current_period_end);
This is the result field coming from stripe in test mode.
current_period_end: 1649650039
But even using new Date(subscription.current_period_end) is coming back as 1970-01-20T02:14:10.039Z
Is this not the field that is suppose to show when the next billing date is ?
Any thoughts? What am I missing ?
UPDATE:
I was just doing some testing and figured out if I multiply that value by 1000 it comes out to be 1649650039000 which equates to
Mon 11 April 2022 00:07:19
Is there a reason for this? Is this a safe method moving forward?
UPDATE:
I accepted answer below and am providing momentjs that converts directly to unix timestamp.
const date = moment(new Date()).unix();
Issue
Stripe reports date fields as Unix timestamps. These represent a date/time as the number of seconds since January 1, 1970 (kinda...leap seconds are weird).
The Javascript Date object attempts to convert the number of milliseconds since January 1, 1970 as that is an increment of time that is more relevant to front-end web coding.
Solution
You have already discovered an adequate solution, that is multiply the timestamp by 1000 and thereby convert the value in seconds to a value in milliseconds. This appears to be a common work around 1, 2, 3
Related
I'm building a course manager API with Angular and Nodejs express and I'm stuck trying to figure out:
I'm trying to figure out how to create a method/function that receives 2 dates, (a time frame -> 1.1.2021 - 30.1.2021) and a weekday (let's say Monday).
And returns an array of all the Mondays between the time frame.
Example:
Course start date: 1.1.2021
Course end date: 30.1.2021
Course day: Monday
And It will return: [4.1.2021,11.1.2021,18.1.2021,25.1.2021,4.1.2021] - those are the dates of all the Mondays in the time frame of 1.1.2021 - 30.1.2021
Could someone give me a hint on how to do that? I tried to use the Date range picker but got nowhere. Is there any built-in function that I can use ?
enter image description here
Check out date-fns
eachDayOfInterval({start, end}).filter(z => getDay(z) === day)
Where start and end are normal Date objects, and day is the weekday (0-Sunday, 1-Monday, etc.)
I need to compute the number of days between two dates using NodaTime, and to do it in a timezone.
The end time is date based with an implied time of midnight at the end of the day. This date is in a timezone.
The start time is the current time, but I am passing it into the function so that the function is testable.
I tried using Period, which seems like the obvious answer, but Period is too granular on one end (when we are on the end day) and not granular enough when we are more than 1 month away.
So if Now is July 9, 5:45pm in America/Toronto and the End Time is Sept 1, 00:00:00, then I would like to be able to calculate 54 days. (assuming I counted the number of days on my calendar correctly. :) )
I figured I would have to handle the sub day problem myself, but it surprised me when I had to figure out how to handle the greater than a month problem.
Is there an obvious way to get the number of days between two times from NodaTime? I have it down to three lines of code using .Net's DateTime and TimezoneInfo classes, but I want to move to NodaTime for all the reasons specified on the site.
Thanks
I should have read the Arithmetic section of the docs more closely before posting.
You can specify which unit you want the math result to be in with a 3rd parameter. Here is what I needed:
Period timeLeft = Period.Between(nowInTz.LocalDateTime, endDate, PeriodUnits.Days);
this is from the docs:
http://nodatime.org/unstable/userguide/arithmetic.html
Hope this helps somebody else in the future.
I am using sharepoint 2013 and am creating a document view to show contracts that are going to expire within 30 days of today. I have a column called expiry date. Example, if today was Jan 15th, I want to show all contracts from Dec 15th to today.
How can I create a filter to do this?
I have tried expiry date less than or equal to [Today] - 30 but that didn't work as it bring back incorrect data.
The filter should read:
Less Than or Equal To
[Today]+30
It will not accept spaces in the formula. [Today]-30 would be stuff that had already expired.
I was having a time with this as well. Try this
Due Date is less than or equal to [Today]+30
AND
Due Date is greater than or equal to [Today]
Filter should be (based on your column name above):
Expiry Date > = Today and <= Today+30
This starts with the beginning date to the latest date (30 days later)
I have an Excel spreadsheet that is being read.
The value in the spreadsheet is 7/24/2014 10:43:33 AM
The cell value after being read using OpenXML is 41844.446908680555.
When I do this calculation to convert to a date:
dte = DateTime.FromOADate(double.Parse(value));
I get 7/24/2014 10:43:32 AM
Is this typical when converting date/time or am I missing something?
Thanks
It seems that DateTime.FromOADate(double.Parse(value)); is truncating rather than rounding the fractional seconds. Excel stores date/time as days and fractions of days since 1 Jan 1900 (with the intentional error of calling 1900 a leap year, supposedly for compatibility with Lotus 123 at the time).
Therefore, the number 41844.446908680555 translates to, given Excel's level of precision
7/24/2014 10:43:32.910
(actually: 7/24/2014 10:43:32.9099949030205)
Just format the cells as Dates. 41844.446908680555 is Excel's way of serializing the date value.
When Excel stores a date or time in stores it in a number format with the date January 1, 1900 = 1
so really when you’re storing a date with the date format you’re really just storing the numeric value of difference between the date and January 1, 1900
So for example 365 = jan-30-1900
And fractions of a number equal parts of the day so .5 = half a day or 12 hours.
And for the fun of it right now = 41885.75 or sept-3-2014 at 6PM or 41885.75 from jan 1 1900.
The reason why this is done is to now allowed dates to be used in mathematical functions. And it deals with a lot of problems that pop-up with dates such as leap year and also provides for easier ways to deal with time zones as well.
I'm using Node.js, Postgres, and the node-postgres library. When I try to insert a new record with the current date with code like this:
client.query('INSERT INTO ideas(date) VALUES($1)', [new Date()], ...);
And this runs fine with no errors. However, when I run a select statement against the database and log the results, the date I'm given appears as:
Wed Nov 20 2013 19:00:00 GMT-0500 (EST)
Which is fine, except that when I inserted the record, it was Thursday, November 21. And the time was 5:47, not 7:00 as the output would suggest.
I ran the code a few more times, and it stored the same inaccurate date no matter the time, even once the next hour had begun. This leads me to believe that for some reason, it's only storing the date and not the hour or minute. In addition, the fact that the date is only off by one day suggests that the problem might have something to do with the way node-postgres handles dates.
I know it's not a problem with Javascript calculating the current date when passing it into the query, because I logged new Date() and it was accurate, to the date, minute, hour, and second.
Any help on this issue would be greatly appreciated. Thanks!
The problem (thanks to Craig for pointing this out) was that I was using the date type for my Postgres column, which only records dates, and not times. Once I switched to the timestamptz type everything worked perfectly!