Apex - Subtract minutes from DateTime Object
-
23-12-2019 - |
Question
I'd like to subtract 5 minutes from DateTime.now() May I know how I can do this?
Thanks!
Solution
It has add
in it's name but nobody says you can't pass a negative number.
DateTime dt = System.now();
DateTime earlier = dt.addMinutes(-5);
DateTime fullHour = dt.addMinutes( -dt.minute() ).addSeconds( -dt.second() );
System.debug(dt + '\t' + earlier + '\t' + fullHour);
2014-01-11 08:40:13 2014-01-11 08:35:13 2014-01-11 08:00:00
OTHER TIPS
Just subtract time in days (Double format) from DateTime.now()
as follows:
i.e. 5 (minutes) = 5 / 1440 (days)
DateTime dt = DateTime.now() - Double.valueOf(5) / 1440;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow