2

I hope you guys can help me. I'm developing a shopping page about online courses in Elementor. These courses start every 2 months and I want to add a feature that changes the price when there are only 19 or less days left to purchase the course before it starts. I managed to solve the problem only for certain cases, for example: The course begin on february 28th and todays date is february 17th. So by using the "new Date()" object and then adding the .getDate() method, I got the current day. Then, to grab the course date, I had to use the method parseInt("28th february"), (I'm Spanish so the number comes first). Once I had both dates I wrote an if statement saying:

if ((courseDate - currentDate) <= 19) { Do this... }

It works if boths dates are in the same month... But, for example, if the course begins the next month, on march 22th, it will also evaluate to true, but there are actually more than 19 days left to purchase... Any ideas about how can I solve it? I appriciate your help and time guys

Alvaro77
  • 23
  • 4
  • 1
    There is of course a way to do this by calculating the difference as milliseconds and then dividing by amount of millis in a single day, but if you plan to have a lot of non-trivial date operations in your app, consider using a date library with intuitive APIs, like moment.js/luxon.js or similar date/time libraries. You really don't want to cover every single edge case when dealing with date and time yourself, there are "years of shared knowledge" in these libraries. – Ma3x Feb 17 '22 at 09:04
  • These APIs sound really interesting, can you tell more about them, or how can I learn to use them? I'm sorry if I sound as a complete beginner, but that's exactly what I am haha – Alvaro77 Feb 17 '22 at 09:42
  • Can you show us an example of the dates you are trying to compare? – phuzi Feb 17 '22 at 10:03
  • @Alvaro77: I made an example with Luxon for your use case here https://jsfiddle.net/huawz608/. To learn more about the APIs, check the links to the docs in code comments. – Ma3x Feb 18 '22 at 03:19

1 Answers1

2

You want to work with the numeric values of dates, i.e., the number of milliseconds since 1 Jan 1970 at midnight. The relevant method is getTime().

Orius
  • 1,084
  • 4
  • 11
  • Thank you for answering. I really don't understand which operation should I do, the getTime() method returns an enormous number, how should I work with it to be precise? – Alvaro77 Feb 17 '22 at 09:39
  • 1
    That number represents the number of milliseconds passed since some fixed time. So if you have two dates, the difference between their `getTime()`s is the difference between those dates expressed in milliseconds. To translate it into days, you just need to divide it by the number of milliseconds in a day, which is 1000*60*60*24 (=86400000). – Orius Feb 17 '22 at 09:44
  • 1
    @Orius That's not always true (unless you work (exclusively with UTC), days where daylight savings start and end will of course be an hour longer or shorter (assuming a 1 hour difference is apllied). – phuzi Feb 17 '22 at 09:56
  • I'm probably mistaken, but the problem is that the date I got from the course is just a normal integer, it is not attached to any clock or actual date because I got it by using the parseInt() method, as it was a string. So I can't use the getTime() method in it because it is not a date, it doesn't have a month or a year defined, just a normal number... I don't know if I explain myself. I'm sorry if I'm driving you crazy – Alvaro77 Feb 17 '22 at 09:57
  • 1
    @phuzi True, but maybe Alvaro77 doesn't need to go into that resolution, or at least could start with this simple solution and only then move on to more complicated ones. – Orius Feb 17 '22 at 10:01
  • @Alvaro77 What does that "normal number" represent? How does it translate into a date? – Orius Feb 17 '22 at 10:02
  • @Orius I have a string that says "28 de Febrero", and I used the parseInt() method to get the 28 number, because I thought it would solve my problem by using the first operation I described if ((courseDate - currentDate) <= 19) { Do this... }. But it didnt work so... The problem is that I'm not comparing two dates, I'm comparing an integer with a date. So the date form the course is not a date, is just a string that displays on the webpage for the user – Alvaro77 Feb 17 '22 at 10:11
  • `parseInt("28 de Febrero")` returns 28, as does e.g. `parseInt("28 de Septiembre")`, so you cannot even distinguish between those dates with your current method. You'll have to find a way to parse strings representing dates in Spanish. If you want help with that, I think it's time to ask a separate question, focused on parsing such strings. – Orius Feb 17 '22 at 10:18
  • This should be a comment, it's not an answer. – RobG Feb 17 '22 at 11:25