I'm trying to get the remaining minutes of the current day, but the Datetime class its giving me headaches. (I'm going crazy)

Datetime now = Datetime.now();//System.now();
Integer minutesToAdd = 1440 - ((now.hour() * 60) + now.minute());

And I'm getting a debug like

12:23:14:033 USER_DEBUG [2]|DEBUG|2015-10-14 17:23:14
12:23:14:033 USER_DEBUG [3]|DEBUG|19
12:23:14:033 USER_DEBUG [4]|DEBUG|23
12:23:14:034 USER_DEBUG [6]|DEBUG|277
12:23:14:034 USER_DEBUG [7]|DEBUG|2015-10-14 22:00:14

Why is the hour different in the first and second debug line? Shouldn't it be the same?

In the fifth line, first I'm calculating the minutes that have happened in the day by multiplying the hours (0-24) by 60 and adding the minutes in the current hour, and then I'm subtracting this value to the total minutes in a day (1440).

What I was hopping to validate with the last debug is that I got the remaining minutes of the day in the minutesToAdd variable, it should have been something like 2015-10-15 00:00:14 not 2015-10-14 22:00:14

What am I doing wrong? Or how could I approach this? I need the remaining minutes.

  • It seems like timezone difference is coming into play – Dave Oct 14 '15 at 18:26

Because Now() returns the time in GMT

and Hour() return the hour in Local timezone

So you will want to use

Time t = DateTime.Now().Time()

to get the time in the local timezone of the user

Datetime now = Datetime.now();//System.now();
Time t = now.Time();
Integer minutesToAdd = 1440 - ((t.hour() * 60) + t.minute());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.