0

I am developing an application with React Native. I will take the local date of the user and save it to the database. It doesn't matter to me the hour, minute or second. So I want to reset them. My purpose in resetting is to be able to compare dates more easily. Just let me record the date of that day correctly is enough for me. The code below looks fine to me but I'm not sure of its correctness.

There are a lot of ideas about date operations, but there are time differences according to countries, summer time, winter time, etc. I wanted to open this topic beforehand in order not to have problems with such issues.

    const today = new Date();
    today.setUTCHours(0, 0, 0, 0);
    console.log(today);

    // output: 2023-06-15T00:00:00.000Z
imvain2
  • 15,480
  • 1
  • 16
  • 21
Blogger Klik
  • 160
  • 1
  • 2
  • 12
  • Is it possible for you to save date in DB using Date function? It will only save the date with ` format YYYY-MM-DD` format. – Apoorva Chikara Jun 15 '23 at 16:19
  • @Apoorva Realm supports it. – Blogger Klik Jun 15 '23 at 16:23
  • The code posted seems good to me. What issues are you having? – evolutionxbox Jun 15 '23 at 16:24
  • @evolutionxbox Date operations may differ. I opened the issue with the concern that there might be something I can't see :) – Blogger Klik Jun 15 '23 at 16:26
  • @BloggerKlik may differ how? – evolutionxbox Jun 15 '23 at 16:26
  • @evolutionxbox I ll do operations such as day difference between dates. The link has different opinions. Ex. such as summer time and winter t. https://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates – Blogger Klik Jun 15 '23 at 16:42
  • 1
    if you work with everything as UTC then you can have a stable baseline. your server should be UTC as well if you go with that approach. also database. besides, if you don't care for hours, why would you care for daylight saving time? – IT goldman Jun 15 '23 at 20:01
  • If you're worried about time zones, and are infact needing to do comparisons, i find the `dayjs` - https://day.js.org/ library really good, it offers a lot of functionality especially date manipulation and comparison using easy to read functions such as date1.isAfter(date2) Best thing about it is, the whole library is only 2KB !! – Gweaths Jun 15 '23 at 20:32
  • @ITgoldman Now it's past 00:00 and it's day 16. But in my code the day is 15. That's why I can't be sure of its accuracy. – Blogger Klik Jun 15 '23 at 21:10
  • ok so you can work with UTC as your real time, and for each client display according to his own time zone. for example using javascript and browser's default. – IT goldman Jun 15 '23 at 21:14
  • @ITgoldman Actually I want something very simple. "2023-06-15T00:00:00.000Z" How can I get the date in user's phone in this format? Not according to UTC. I want to get the correct date according to the user. – Blogger Klik Jun 15 '23 at 21:19

0 Answers0