Coder Social home page Coder Social logo

Comments (15)

herkyl avatar herkyl commented on June 21, 2024

Could you please write an example of how to replicate it

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

Problem was in date object.
new Date('2016/10/01 00:00:00') — works wrong (switch timezone — correct)
new Date(2016, 10, 1, 0, 0, 0) — works correct (doesn't switch timezone — wrong?)

Can you advise some overall rules for working with Date object?

screen shot 2016-10-12 at 9 56 40 am

https://jsbin.com/pojeveratu/edit?js,console

from instadate.

herkyl avatar herkyl commented on June 21, 2024

Date timezone is in the local timezone of the user. I recommend not using 00:00:00 times since you can lose a day due to daylight saving time. You can use instadate.noon to prevent that.

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

So I should to parse time string like 2016/10/01 14:33:13 and set every parameter as argument of new Date() for correct work? How I can create Date from string without parsing and without incorrect timezone behaviour?

from instadate.

herkyl avatar herkyl commented on June 21, 2024

From where are you getting the dates? You can use the ISO 8601 format to safely set them. Most APIs should be returning that. There is no way around the default timezone when using Date (or instadate since it depends heavily on Date). If timezones are important I recommend checking out Moment.js for that part.

If you are generating dates on client-side JS code I recommend creating a base instadate.noon date and using instadate to manipulate that (add days, etc).

Sorry for the late replies btw, I have so many GH notifications that I rarely read them..

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

Oops, I have mistake in example
https://jsbin.com/nudalik/1/edit?js,console
With new Date(2016, 9, 1, 0, 0, 0), I have same mistake in dates array

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

I don't want use timezones, but also I don't want any side effects from them

from instadate.

herkyl avatar herkyl commented on June 21, 2024

What are you trying to do exactly? If you don't care about timezones instadate should work. Why are you trying to use the format 2016/10/01 00:00:00?

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

After using dates()function I expect to get array of unique dates, but I got duplicates :(

from instadate.

herkyl avatar herkyl commented on June 21, 2024

This is because of Daylight savings clock turning on October 30 (http://www.webexhibits.org/daylightsaving/b.html).

You can fix this by using the noon function from instadate:

const dates = instadate.dates(
  instadate.noon(new Date('2016/10/01 00:00:00')),
  instadate.noon(new Date('2016/11/01 00:00:00'))
);

If you are using the 00 hour of the day then daylight saving can shift it to the previous day.

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

But setting to noon ruins my time!

from instadate.

herkyl avatar herkyl commented on June 21, 2024

You have to find a way around it, as it is a limitation of the JS Date.

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

But you can avoid this situation, just don't use 24 hours constant as day length. Instead you can use a function like

function addDays(date, days) {
    var result = new Date(date);
    result.setDate(result.getDate() + days);
    return result;
}

from instadate.

herkyl avatar herkyl commented on June 21, 2024

I added tests for the edge case as well as changed all of the adding functions in ce0352a and published it on npm as 0.3.1.

Now I wonder if differenceInX functions also have that issue. Need to check those later.

Let me know if this fixes your issue

from instadate.

max-mykhailenko avatar max-mykhailenko commented on June 21, 2024

Thanks! It's great, I'll test it on this week

from instadate.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.