How to Set the Date and Time with Unix Epoch Format

The Unix epoch started at 00:00:00 UTC on Thursday, January 1, 1970. Ever since then Unix systems have kept track of time by counting the number of seconds since that date occurred. Unix, and the various implementations like Linux and FreeBSD, keep track of time as a literal number of seconds since then minus the number of leap seconds that have taken place.

It’s not a concept that many users or even programmers come into contact with on a daily basis. Nevertheless, if you know the number of seconds that have elapsed since the beginning of the Unix epoch, you can actually set the time on your system to it. You’ll need to be working on a command line interface, so either hold down Ctrl, Alt and F1-F6 to get to a virtual terminal or push Ctrl+Alt+T to get a graphical one. You can also search for the word terminal on the Ubuntu Dash or start it from the Applications and then System Tools menu in LXDE, KDE and off the Whisker Menu in Xfce4.

Method 1: Using the GNU date Tool to set Unix Epoch time

It’s a good idea to make sure that you have the date string formatted correctly before you attempt to set the clock to it. Type date -d ‘@1501959335’ and push enter to convert the number of seconds from January 1, 1970 into a format preferred by people. You can replace 1501959335 with any valid Unix epoch time stamp. We simply used that for an example because it was the current Unix epoch time at one point while writing this article.

You should get a regular date and time back as well as the time zone for your local machine. When you’re sure you have things right, you can use the command date -s ‘@1501959335’ to set the clock to this time stamp. If you receive an error that reads “date: cannot set date: Operation not permitted” followed by the current date, then you were attempting to run it as a user. Type sudo date -s ‘@1501959335’ and push enter to set the clock. Remember to use a valid Unix time stamp in place of the digits that we featured in our example.

Naturally, you’ll be asked your password before continuing to receive root access.

Method 2: Using BSD date Software

If you’re using any of the various *BSD operating systems, then you’ll actually need to use a different syntax when it comes to the date command. This goes for users of FreeBSD, OpenBSD, NetBSD and possibly some implementations of Darwin as well. Users of Linux and other Unix-like operating systems

To convert the seconds since the epoch started on January 1, 1970 type date -r 1501959335 at the prompt and push enter. Once again, you can replace 1501959335 with any valid Unix time stamp.

Type date “$(date -r 1501959335 +’%y%m%d%H%M.%S’)” and push enter to set the date since the epoch started. It’s a bit more complex in these BSD-based operating systems since you have to tell it the new format for the date and time, but it ultimately works the same way in the end. You will more than likely need root access to set the new date.

Method 3: View the Current Unix Time

If you’d like to see the current Unix epoch time stamp, then run date +%s from the command line. This will output the current time as the number of seconds since the Unix epoch began. You’ll have it quickly returned for you on the next line.

You can actually get a graphical representation as well if you’d prefer. Type xclock -d -utime at the command line and push enter. Provided that you have the classic XFree86 apps installed, you’ll receive a window floating over your terminal that gives you the current count of seconds.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.