Cyberithub

How to Convert/Change Time to Epoch Time using date utility on Linux or Unix Server{13 Best Examples}

If you are are programmer or developer then you might have felt or encountered the necessity to change time to epoch time using date utility in Linux or Unix Server. It is not very uncommon to use the epoch time in Linux or Unix friendly scripts or programs hence it is very important to understand the usage of date command to achieve this task. date is an open source Linux/Unix based commands to check the System date and time as per the set timezone.

What is Epoch Time

Epoch time is also known as Unix Time or POSIX Time. It is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), minus leap seconds.

How to Convert/Change Time to Epoch Time using date utility on Linux or Unix Server

How to Convert/Change Time to Epoch Time using date utility on Linux or Unix Server

Also Read: How to Install jq(JSON Processor) on RHEL/CentOS 7/8 

Example 1: How to Convert/Change Current date and time to Epoch Time in seconds

You can convert current date and time to epoch time in seconds by using date +%s command as shown below.

[root@localhost ~]# date +%s
1605078458

%s : seconds since 1970-01-01 00:00:00 UTC. More on date command Man Page.

Example 2: How to Convert/Change Current date and time to Epoch Time in nanoseconds

You can convert current date and time to epoch time in nanoseconds by using date +%s%N command as shown below.

[root@localhost ~]# date +%s%N
1605078761438541981

%N : nanoseconds (000000000..999999999). More on date command Man Page.

Example 3: How to Convert/Change Current date and time to Epoch Time in milliseconds

You can convert current date and time to epoch time in milliseconds by using date +%s%N | cut -b1-13 command as shown below.

[root@localhost ~]# date +%s%N | cut -b1-13
1605078601826

Example 4: How to Convert/Change “24 hours ago” date and time to Epoch Time in seconds

If you want to convert 24 hours ago date and time to epoch time in seconds then you need to use date --date="24 hour ago" +%s command as shown below.

[root@localhost ~]# date --date="24 hour ago" +%s
1604994189

--date : display time described by STRING, not ‘now’. More on date command Man Page.

Example 5: How to Convert/Change “24 hours ago” date and time to Epoch Time in nanoseconds

If you want to convert 24 hours ago date and time to epoch time in seconds then you need to use date --date="24 hour ago" +%s command as shown below.

[root@localhost ~]# date --date="24 hour ago" +%s%N
1604994344408376989

Example 6: How to Convert/Change “24 hours ago” date and time to Epoch Time in milliseconds

If you want to convert 24 hours ago date and time to epoch time in seconds then you need to use date --date="24 hour ago" +%s command as shown below.

[root@localhost ~]# date --date="24 hour ago" +%s%N | cut -b1-13
1604994361301

Example 7: How to Convert/Change “a week ago” date and time to Epoch Time in seconds

If you want to convert a week ago date and time to epoch time in seconds then you need to use date --date="1 week ago" +%s command as shown below.

[root@localhost ~]# date --date="1 week ago" +%s
1604476293

Example 8: How to Convert/Change “a week ago” date and time to Epoch Time in nanoseconds

If you want to convert a week ago date and time to epoch time in nanoseconds then you need to use date --date="1 week ago" +%s%N command as shown below.

[root@localhost ~]# date --date="1 week ago" +%s%N
1604476297962846178

Example 9: How to Convert/Change “a week ago” date and time to Epoch Time in milliseconds

If you want to convert a week ago date and time to epoch time in milliseconds then you need to use date --date="1 week ago" +%s%N | cut -b1-13 command as shown below.

[root@localhost ~]# date --date="1 week ago" +%s%N | cut -b1-13
1604476314309

Example 10: How to Convert/Change “a month ago” date and time to Epoch Time in seconds

If you want to convert a month ago date and time to epoch time in seconds then you need to use date --date="1 month ago" +%s command as shown below.

[root@localhost ~]# date --date="1 month ago" +%s
1602403631

Example 11: How to Convert/Change “a month ago” date and time to Epoch Time in nanoseconds

If you want to convert a month ago date and time to epoch time in nanoseconds then you need to use date --date="1 month ago" +%s%N command as shown below.

[root@localhost ~]# date --date="1 month ago" +%s%N
1602403636494139434

Example 12: How to Convert/Change “a month ago date and time” to Epoch Time in milliseconds

If you want to convert a month ago date and time to epoch time in milliseconds then you need to use date --date="1 month ago" +%s%N | cut -b1-13 command as shown below.

[root@localhost ~]# date --date="1 month ago" +%s%N | cut -b1-13
1602403644437

Example 13: How to Check Other Options available with date commands

If you want to check all the options available with date command then you need to use date --help command as shown below.

[root@localhost ~]# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING display time described by STRING, not 'now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date
and time to the indicated precision.
-r, --reference=FILE display the last modification time of FILE
-R, --rfc-2822 output date and time in RFC 2822 format.
Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC='date', 'seconds', or 'ns' for
date and time to the indicated precision.
Date and time components are separated by
a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time (UTC)
--help display this help and exit
--version output version information and exit

 

 

 

 

Popular Recommendations:-

How to Defragment an XFS Filesystem in Linux(5 Simple and Effective Steps)

How to Install Arpwatch tool on RHEL/CentOS 7/8(Simple and Effective Steps)

How to Install and Configure Squid Proxy Server on RHEL/CentOS 7/8

Python3: ModuleNotFoundError: No Module Named “prettytable” in Linux 

How to List all the Installed Python Modules in Linux{2 Easy Methods}

Solved: ModuleNotFoundError: No Module Named “requests” in Python 3

How to Install and Enable EPEL Repository on RHEL/CentOS 7/8{Simple and Easy Steps}

Solved: FATAL: Authentication Helper Program /usr/lib/squid/basic_ncsa_auth: (2) No Such File or Directory

Leave a Comment