0

What I intent to get is

$ xxx 2019-10-11 <= insert your command
1570752000

The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd

Justin Lin
  • 673
  • 1
  • 6
  • 15

2 Answers2

1

This should work:

date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'

-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • I tried your command on osx. the output seems not right ```(master) * [origin ] $ date -j -f '%Y-%m-%d' '2019-10-11' '+%s' 1570837643``` and every time it gives a different number – Justin Lin Oct 14 '19 at 23:48
0

On NetBSD the following will work:

TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'

Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).

On MacOS you might try something similar to what Oguz suggested:

TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'
Greg A. Woods
  • 2,663
  • 29
  • 26