question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

datenum() not accounting for DST properly

See original GitHub issue

Creating this issue as recommended in #832.

Here is a jsfiddle to witness the problem: https://jsfiddle.net/gtt2srcu/

I have some problems with the datenum function, not behaving correctly when the v is not in the same timezone offset as the current time (ex. current time has DST offset and v does not have DST offset). After playing around with the code, I believe the offsetting needs to be dependent on the v value instead of current time, like this:

var basedate = new Date(1899, 11, 30, 0, 0, 0);
var basetimestamp = basedate.getTime();
var baseoffset = basedate.getTimezoneOffset();
function datenum(v/*:Date*/, date1904/*:?boolean*/)/*:number*/ {
	var epoch = v.getTime();
	if(date1904) epoch -= 1462*24*60*60*1000;
	// Account for offset differences between the basedate and v...
	var adj = (v.getTimezoneOffset() - baseoffset) * 60000;
	return (epoch - basetimestamp - adj) / (24 * 60 * 60 * 1000);
}

This is because if basedate and v have a different offset, we need to compensate this effect by applying the difference on the numerical value provided to excel. The offset of the current time does not seem relevant.

It’s possible that the numdate function suffers from the same problem, but I haven’t digged through it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
EBMikecommented, Nov 6, 2017

@SheetJSDev Maybe add a third date “type” which is just an iso-date string. Not everyone needs a “time” on their dates. In fact, for our code, every datetime we encounter is broken into 2 excel columns (for date and time).

To respond to the server timezone vs client timezone comment above… yes, thats the problem we were encountering that become much harder after the change noted in #832. I did not bring it up in the issue because I didn’t think it was relevant at the time, and the issues noted in #832 would be obvious enough.

0reactions
SheetJSDevcommented, Sep 15, 2021

v0.16.0 changed date handling. Please re-test against a version at or above v0.16.0 and report back if issues persist

Read more comments on GitHub >

github_iconTop Results From Across the Web

MATLAB datenum - MathWorks
The datenum function does not account for time zone information in t and does not adjust datetime values that occur during Daylight Saving...
Read more >
Daylight Savings Time not calculated properly in Rails 2.3.5?
Since the DST calculation depends on the date, this is probably where you're losing the information. (January 1st is not in DST, so...
Read more >
bug #36954, datenum / datevec convert... - GNU Savannah
According to the Octave help, datenums “do not take into account time zones […] ... The fix could simply be making strptime() interpret...
Read more >
How do you account for daylight savings time in a formula?
... by implementing a formula that accounts for DST but I have not been ... implement because it does not convert correctly when...
Read more >
global real time converter
Explore the account used by 13 million people to live, work, travel and transfer money ... Daylight Saving Time is not in effect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found