[Noob question] Get an hour of a date

Hi all!

I want to know how I can get the start of a day/a particular hour.

i.e.: I need to filter a list with all the elements that the attribute ‘date’ is after 11:00 am, local timezone (we can assume timezone = GMT+00:00).

I couldn’t find the way to make this… I prefer to do it without any other library than elm/time (for learning purposes), but if is complicated I can add a package.

I don’t have any problem getting the current date. So, I need to know how to make a function that, given a Posix time, I can get the 11:00 am date for that exactly day (also in posix).

Thank you folks!

1 Like

If you decide to try other packages, you could consider this:

        waratuman/time-extra

and use the function Time.Extra.startOfDay

after that you can add 11 hours * 60 minutes * 60 seconds to that. This will not work on days where the daylight savings time changes.

1 Like

i think you will need to use functions such as toMinute. the documentation of some of these functions suck, so here’s an example

https://ellie-app.com/czdR5rcfLK6a1

1 Like

I’d highly recommend using justinmimbs/time-extra, if you go with a package, as it will cover almost everything you need.

If you decide to stick with just elm/time then I’d recommend looking at how that package does floor as this function takes a time zone, a posix value, and an interval (Day, Hour, etc) and gives your the start of it. There’s a lot of other goodies in the source code too around building up specific date-times.

2 Likes

Thank you to all. At the end I used the answer provided by @wolfadex and @aklaing, with waratuman/time-extra package :smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.