Playing with timestamp and timezone formats in Linux

Getting started

Hi folks, today I am going to share few tips on customizing date and time formats which will be very helpful while generating random codes based on time-stamp, also I will show you how to convert time-zone in Linux which will be useful in standardizing time-stamp.

Customize date format

You can display customized date format as you wish. I will show you some standard formats which I usually prefer. You can also pick up your date format using the parameters shown at the end of this article (fetched from  date manual page)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ date '+%d%m%y'
040318
$ date '+%d%m%Y'
04032018
$ date '+%Y%m%d'
20180304
$ date '+%y%m%d'
180304
$ date '+%D'
03/04/18
$ date '+%x'
Sunday 04 March 2018
$ date '+%d-%m-%Y'
04-03-2018
$ date '+%d %b %Y'
04 Mar 2018

Customize time-stamp format

You can display customized time-stamp format as you wish. I will show you some standard formats which I usually prefer. You can also pick up your time-stamp format using the parameters shown at the end of this article (fetched from  date manual page)

1
2
3
4
5
6
7
8
9
10
11
12
$ date '+%T'
03:26:48
$ date '+%r'
03:26:48 AM IST
$ date '+%R'
03:26
$ date '+%X'
03:26:48 IST
$ date '+%H%M%S'
032648
$ date '+%H:%M:%S'
03:26:48

Converting timezone (TZ)

By default, we can convert any time zone into UTC which is considered as a standard time-zone, because the time-zone conversion derives from UTC time-zone by simply passing -u (or) –utc arguments to date.

1
2
3
4
5
6
$ date
Sun Mar 4 03:53:27 IST 2018
$ date -u
Sat Mar 3 22:23:27 UTC 2018
$ date --utc
Sat Mar 3 22:23:27 UTC 2018

For example say IST inherits from UTC+0530. Lets jump into the topic on how to convert any timezone to some other time-zone using BASH. We are going to pass the time-zone details into TZ temporarily along with date arguments as shown below. To get TZ details, see /usr/share/zoneinfo/

1
2
3
4
$ TZ=America/Los_Angeles date
Sat Mar 3 14:34:02 PST 2018
$ date
Sun Mar 4 04:04:03 IST 2018

Few interesting workaround examples

I am happy to share few interesting workaround examples using date commands with you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
date -d '2 month ago'
date -d '4 week ago'
date -d '6 day ago'

date -d 'last friday'
date -d 'next friday'

date -d 'last month'
date -d 'next month'

date -d 'last week'
date -d 'next week'

date -d 'yesterday'
date -d 'tomorrow'

Date format controls (from manual page)

I have pasted the date format control arguments fetched from date manual page for reference. you can also view this page from your Linux terminal by typing man date (or) date –help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 %% a literal %
 %a locale's abbreviated weekday name (e.g., Sun)
 %A locale'
s full weekday name (e.g., Sunday)
 %b locale's abbreviated month name (e.g., Jan)
 %B locale'
s full month name (e.g., January)
 %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
 %C century; like %Y, except omit last two digits (e.g., 20)
 %d day of month (e.g., 01)
 %D date; same as %m/%d/%y
 %e day of month, space padded; same as %_d
 %F full date; same as %Y-%m-%d
 %g last two digits of year of ISO week number (see %G)
 %G year of ISO week number (see %V); normally useful only with %V
 %h same as %b
 %H hour (00..23)
 %I hour (01..12)
 %j day of year (001..366)
 %k hour, space padded ( 0..23); same as %_H
 %l hour, space padded ( 1..12); same as %_I
 %m month (01..12)
 %M minute (00..59)
 %n a newline
 %N nanoseconds (000000000..999999999)
 %p locale'
s equivalent of either AM or PM; blank if not known
 %P like %p, but lower case
 %r locale's 12-hour clock time (e.g., 11:11:04 PM)
 %R 24-hour hour and minute; same as %H:%M
 %s seconds since 1970-01-01 00:00:00 UTC
 %S second (00..60)
 %t a tab
 %T time; same as %H:%M:%S
 %u day of week (1..7); 1 is Monday
 %U week number of year, with Sunday as first day of week (00..53)
 %V ISO week number, with Monday as first day of week (01..53)
 %w day of week (0..6); 0 is Sunday
 %W week number of year, with Monday as first day of week (00..53)
 %x locale'
s date representation (e.g., 12/31/99)
 %X locale's time representation (e.g., 23:13:48)
 %y last two digits of year (00..99)
 %Y year
 %z +hhmm numeric time zone (e.g., -0400)
 %:z +hh:mm numeric time zone (e.g., -04:00)
 %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
 %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
 %Z alphabetic time zone abbreviation (e.g., EDT)

That’s all for now folks 🙂 Stay tuned with us to get instant update! Still confused on formatting date? Let us know through comment section, we will help you out — See you all in the next article. Until then, this is Karthick from TechieDrone signing off 😉

Spread the word :)

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of