26

I need to parameter-ize a datetime value with an objective of passing to a constructed URI to make a Smartsheet API call to get data (i.e. sheets) changed in last 24 hours.

I want to use Linux date command as I can do something like date -d '1 day ago' %F to get the output of a day before today. How can I use the date command to convert the value to yyyy-MM-dd'T'HH:mm:ss'Z' format to get something like 2018-01-01T00:00:00-07:00?

If the value is not in this particular format, then Smartsheet API complains:

HTTP_01 - Error fetching resource. Status: 400 Reason: 
Bad Request : { "errorCode" : 1018, "message" : "The value '/home/my/path/to/param_file/Sysdate' was not valid for the parameter modifiedSince.", "refId" : "1xqawd3s94f4y" }

Thanks,

Allan
  • 12,117
  • 3
  • 27
  • 51
Vlad
  • 267
  • 1
  • 3
  • 3
  • Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/).* – jww Mar 09 '18 at 12:26
  • @jww, did you downvote all the answers? If yes, why? – randomir Mar 09 '18 at 16:45

4 Answers4

32

To output date in ISO 8601 format, you'll probably want to use -I[FMT]/--iso-8601[=FMT] option, if your date supports it (GNU/Linux version does). The FMT is basically a resolution, and in your case you'll want to use s for seconds:

$ date -Is
2018-03-09T09:28:14+01:00

The alternative (for POSIX date, including BSD/OSX date) is to explicitly specify the format and output the time in UTC time zone (-u flag):

$ date -u +"%Y-%m-%dT%H:%M:%SZ"
2018-03-09T08:28:14Z

Note the importance of -u and the explicit Z we can append in that case. Without -u, we would need to output the exact time zone in +hh:mm format, but POSIX date provides support only for time zone name output (%Z). GNU date extends the format set with %:z which outputs the numeric time zone, but if already using GNU date, the first approach with -Is is simpler.

randomir
  • 17,989
  • 1
  • 40
  • 55
5

When calling date in your shell use the following format

date +"%Y-%m-%dT%H-%M-%SZ"

2018-03-09T07-44-39Z

Evya
  • 2,325
  • 3
  • 11
  • 22
  • 4
    Please note this prints the correct time *only if your local timezone is UTC*. The problem with this command is that it always prints the local time and appends the hardcoded/fixed timezone, Z. Check out the `-u` flag (see my answer). – randomir Dec 14 '19 at 23:09
4

To be shortest as possible :

date -u +"%FT%TZ" for UTC

date +"%FT%TZ" for locale's time

jadjay
  • 41
  • 2
  • 1
    This is wrong - the `Z` is an indicator that it is a UTC timestamp. It produces a valid format but changes its meaning. – Waddles Nov 04 '21 at 01:06
  • jadjay's response is helpful, but as @Waddles points out, it's not entirely correct. From the `man date` output on Ubuntu: *%F* gives us the full date; just like `%+4Y-%m-%d` does. *T* gives us the character "T". *%T* gives us the time; same as `%H:%M:%S` . *Z* gives us the character "Z". This leaves us with: `2022-07-09T12:24:06Z` , which is in local time, not Zulu. The *-u* converts to UTC (a.k.a. zulu time). Read the man page `man date` for a lot more info. – AFK Jul 09 '22 at 19:32
  • All right, all right, all right gentlemen : Let say I wrote `date -u +"%FT%TZ"` for UTC `date +"%FT%T"` for localtime – jadjay Nov 10 '22 at 07:56
2

Use this alias in the bash_profile:

alias utc='date -u +"%Y-%m-%dT%H:%M:%SZ"'

Then, you can run it like:

$ utc

The output will be:

$ 2021-04-15T01:36:31Z
Arefe
  • 11,321
  • 18
  • 114
  • 168