Node-postgres date not saving accurately - node.js

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!

Related

How to get stripe subscription current_period_end as date

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

Excel Date function and Time function in one formula

I have the following formula
DATE(YEAR(E687);MONTH(E687);DAY(E687))
and I want to use the TIME function as well. E.g:
DATE(YEAR(E687);MONTH(E687);DAY(E687)) - Time(10;0;0)
The reason is that I have guys working on my Excel sheetfrom CAT and PST
I am getting a date from the server and if a PST user adds data I want to minus 10 hrs from the server date to ensure I get the correct data in CAT.
I know my date above only shows date and not time but I want to remove 10hrs so see if it was done the previous day or the same day.
As comments on post suggests make sure your excels separation symbol e.g ; or , - Check regional settings.
Worked!

Attributing values depending of another value w/ VBA ? Excel 2010

First of all, sorry if it already exists but I can't find the words in English to describe rapidly my question, hence I don't know how to search for it. Skip to third paragraph if you only need the question.
I'm using Excel to build a complete document about my work hours (arrived at, finished at, if I've been late and of how long was I late, overtime, etc etc).
I'm using VBA to smooth this task.
My workplace cuts everything into quarter of hours, which means that if you're late by one minute, you'll lose 15 minutes. If you work overtime for 14 minutes, it won't be seen as overtime. If I point at 07:45 (I'm using 24H format, not AM/PM) I'm fine. If I do it at 07:46 it'll be counted as 08:00. Same with 16:14, it'll show up as 16:00 (details are shown but on the corrected sheet used for salary, it'll show this way). If I do it at 16:15 I've confirmed my 15 minutes of overtime.
So while I could do some workarounds, what I've got in mind is shitcode, really. I'd like to know a good (clean) way to do this : I want for every timestamps I input to be cut in degressive quarter of hours (for instance, I want 17:12 to show up as 17:00 or 08:57 as 08:45).
Thanks for your help, if you didn't understand something or doubt about something I've said, it's probably because of the way I explained it, please ask whatever's needed :)
Update 01: Values are indeed Time processed by Excel, not Strings looking like a timestamp.
Update 02: Solved by #FunThomas with TimeSerial-command and some explanations. Thanks, I hope this can be of help for future reference, however I lack the words to write a better title for this problem.
Assuming that you data is really time (and not a string that looks like time) and is stored in Cell A1, you can use a formula like this:
=TIME(HOUR(A1), INT(MINUTE(A1)/15) * 15, 0)
The time-command gets three paramater (hour, minute, second).
The hour-part is simple.
For the minute, we make an integer division by 15 (resulting in values from 0..3) and multiply it back with 15 (to get 0, 15, 30 or 45).
The second is simply set to 0.
Update:
As VBA-function:
Function roundTime(t As Date) as date
roundTime = TimeSerial(Hour(t), Int(Minute(t) / 15) * 15, 0)
End Function

How to get number of days between two dates in nodatime

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.

setHours() function on Heroku

I have written a node worker that gets instagrams and tweets and puts them into a database. It's on a heroku schedule that first checks to see if the todays date exists in the database, if it does then I overwrite it with the new data to make sure that day has no duplicate entries.
I'm setting the date with:
new Date(d).setHours(0, 0, 0, 0);
When I do this on my local machine I get todays date Nov 11th:
todays date 1447228800000
When I do this on heroku I get the day before Nov 10th and the hours and minutes are not 0:
todays date 1447200000000
So when I go to fetch the data for that day and try to display I am getting the day before. Does anyone know why this is happening?
Timezone, needed to update my timezone on heroku. This question is dumb.

Resources