Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. Most common Bash date commands for timestamping
  2. From time to time I get asked how to use the date command to generate a timestamp. Here is an idiot-friendly script you can post for reference in your team’s bin/ if you get interrupted about timestamp questions or have an aversion to typing phrases like “man date” (with or without a space).
  3.  
  4. All but the first one and last three produce filename-friendly strings.
  5.  
  6. A big thanks to the following folks for pointing out mistakes and suggesting useful format inclusions:
  7.  
  8. 2013-05: Rich for the reminder to include UTC and timezoned stamps.
  9. 2017-10: “Hamilton and Meg” (haha!) for pointing out I had my 4 year example formats messed up and for prodding me to include a 2-year example.
  10. 2018-01: Autumn Gray for suggesting that I add examples of including short and long days of the week.
  11. #! /bin/bash
  12.  
  13. # An overly obvious reference for most commonly requested bash timestamps
  14. # Now all you Mac fags can stop pestering me.
  15.  
  16. cat << EOD
  17. Format/result | Command | Output
  18. --------------------------------+----------------------------+------------------------------
  19. YYYY-MM-DD_hh:mm:ss | date +%F_%T | $(date +%F_%T)
  20. YYYYMMDD_hhmmss | date +%Y%m%d_%H%M%S | $(date +%Y%m%d_%H%M%S)
  21. YYYYMMDD_hhmmss (UTC version) | date --utc +%Y%m%d_%H%M%SZ | $(date --utc +%Y%m%d_%H%M%SZ)
  22. YYYYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z | $(date +%Y%m%d_%H%M%S%Z)
  23. YYYYMMSShhmmss | date +%Y%m%d%H%M%S | $(date +%Y%m%d%H%M%S)
  24. YYYYMMSShhmmssnnnnnnnnn | date +%Y%m%d%H%M%S%N | $(date +%Y%m%d%H%M%S%N)
  25. YYMMDD_hhmmss | date +%y%m%d_%H%M%S | $(date +%y%m%d_%H%M%S)
  26. Seconds since UNIX epoch: | date +%s | $(date +%s)
  27. Nanoseconds only: | date +%N | $(date +%N)
  28. Nanoseconds since UNIX epoch: | date +%s%N | $(date +%s%N)
  29. ISO8601 UTC timestamp | date --utc +%FT%TZ | $(date --utc +%FT%TZ)
  30. ISO8601 UTC timestamp + ms | date --utc +%FT%T.%3NZ | $(date --utc +%FT%T.%3NZ)
  31. ISO8601 Local TZ timestamp | date +%FT%T%Z | $(date +%FT%T%Z)
  32. YYYY-MM-DD (Short day) | date +%F\(%a\) | $(date +%F\(%a\))
  33. YYYY-MM-DD (Long day) | date +%F\(%A\) | $(date +%F\(%A\))
  34. EOD
  35. If executed, it will produce the (obvious) output:
  36.  
  37. Format/result | Command | Output
  38. --------------------------------+----------------------------+------------------------------
  39. YYYY-MM-DD_hh:mm:ss | date +%F_%T | 2018-01-24_13:06:51
  40. YYYYMMDD_hhmmss | date +%Y%m%d_%H%M%S | 20180124_130651
  41. YYYYMMDD_hhmmss (UTC version) | date --utc +%Y%m%d_%H%M%SZ | 20180124_040651Z
  42. YYYYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z | 20180124_130651JST
  43. YYYYMMSShhmmss | date +%Y%m%d%H%M%S | 20180124130651
  44. YYYYMMSShhmmssnnnnnnnnn | date +%Y%m%d%H%M%S%N | 20180124130651170243401
  45. YYMMDD_hhmmss | date +%y%m%d_%H%M%S | 180124_130651
  46. Seconds since UNIX epoch: | date +%s | 1516766811
  47. Nanoseconds only: | date +%N | 174236092
  48. Nanoseconds since UNIX epoch: | date +%s%N | 1516766811175655627
  49. ISO8601 UTC timestamp | date --utc +%FT%TZ | 2018-01-24T04:06:51Z
  50. ISO8601 UTC timestamp + ms | date --utc +%FT%T.%3NZ | 2018-01-24T04:06:51.178Z
  51. ISO8601 Local TZ timestamp | date +%FT%T%Z | 2018-01-24T13:06:51JST
  52. YYYY-MM-DD (Short day) | date +%F\(%a\) | 2018-01-24(水)
  53. YYYY-MM-DD (Long day) | date +%F\(%A\) | 2018-01-24(水曜日)
  54. Note that the last two, short and long day-of-week are dependent on the environment variable LANG. After setting LANG=en_US we wind up with the following:
  55.  
  56. YYYY-MM-DD (Short day) | date +%F\(%a\) | 2018-01-24(Wed)
  57. YYYY-MM-DD (Long day) | date +%F\(%A\) | 2018-01-24(Wednesday)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement