I searched for a replacement of date -d
in Nushell but I didn't find anything.
How can I get the current datetime minus N minutes ? Like this $((date now) - 30 minutes)
You can subtract a duration (like 30min
) from the current date provided by date now
(you can also add a negative duration with the same effect):
(date now) - 30min
# or
(date now) + -30min
So, if your input is an integer representing the amount of minutes, just append "min"
to it as a string, then use into duration
to convert it before subtracting/adding like above:
30 | (date now) - ($"($in)min" | into duration)
# or
-30 | (date now) + ($"($in)min" | into duration)
For the sake of completeness, although not recommended, you could also go down the full arithmetic route by formatting the current date as UNIX timestamp measured in seconds using format date "%s"
, then subtracting/adding the amount of minutes also converted into seconds (i.e. multiplied by sixty), and converting that further into nanoseconds (i.e multiplying again by 1 billion) because that's what into datetime
accepts as numerical input. Use it with the --timezone LOCAL
(or -z l
) option to re-include your local time zone (which got lost with the UNIX timestamp).
30 | (((date now | format date "%s") | into int) - $in * 60) * 1_000_000_000 | into datetime -z l
# or
-30 | (((date now | format date "%s") | into int) + $in * 60) * 1_000_000_000 | into datetime -z l
For super precision, you could even extract the UNIX timestamp directly in nanoseconds using %s%f
as the format string, then subtract/add the amount of minutes also converted into nanoseconds:
30 | ((date now | format date "%s%f") | into int) - $in * 60_000_000_000 | into datetime -z l
# or
-30 | ((date now | format date "%s%f") | into int) + $in * 60_000_000_000 | into datetime -z l