- Get UTC time in seconds
- 5 Answers 5
- date Command in Linux
- 1. Overview
- 2. Introduction to the date Command
- 3. Override the Timezone
- 3.1. Display Date and Time in UTC
- 3.2. Display Date and Time in Another Timezone
- 4. Display Date and Time in Various Formats
- 4.1. Display Date and Time in Supported Formats
- 4.2. Display Date and Time in Milliseconds
- 5. Display Date From a Given String
- 6. Get Relative Date and Time
- 6.1. yesterday and tomorrow
- 6.2. next, ago and last
- 6.3. +x and -x
- 6.4. Get Relative Date and Time Based on a Given Date
- 7. Change the System Time
- 8. Conclusion
- how to get local date/time in linux terminal while server configured in UTC/different timezone?
- 3 Answers 3
Get UTC time in seconds
What am I missing here? After getting an answer saying date +%s was returning UTC time, here are more details about the problem I’m facing now. I’m trying to compare a date written in a file with python. This date is written in seconds in UTC time. And the bash date +%s doesn’t give me the same one. Actually if I’m doing in python time.asctime(time.localtime(date_in_seconds_from_bash)) , I get the current time of Sydney, not UTC. I don’t understand.
5 Answers 5
I believe +%s is seconds since epoch. It’s timezone invariant.
@RomanzoCriminale According to date —help : %s seconds since 1970-01-01 00:00:00 UTC . Adding -u doesn’t give a difference. It would still print based from UTC. This is the reason why you were getting the same values.
Both your answers above are correct. UTC Seconds is the same anywhere in the world, and so is useful for unambigious timestamps. Seconds since 1970 midnight in Sydney could be calculated by taking the difference between that time and UTC zero time (-neg 11 or so hours I suppose), but that number is probably not what you want.
My problem is i’m trying to compare a date written in a file with python.This date is written in seconds in UTC time. And the bash ‘date +%s doesn’t give me the same one. Actually if i’m doing in python time.asctime(time.localtime(date_in_seconds_from_bash)), I get the current time of sydney not UTC. I don’t understand
I bet this is what was intended as a result.
$ date -u --date=@1404372514 Thu Jul 3 07:28:34 UTC 2014
time.asctime(time.localtime(date_in_seconds_from_bash))
where date_in_seconds_from_bash is presumably the output of date +%s .
The time.localtime function, as the name implies, gives you local time.
If you want UTC, use time.gmtime() rather than time.localtime() .
As JamesNoonan33’s answer says, the output of date +%s is timezone invariant, so date +%s is exactly equivalent to date -u +%s . It prints the number of seconds since the «epoch», which is 1970-01-01 00:00:00 UTC . The output you show in your question is entirely consistent with that:
date -u Thu Jul 3 07:28:20 UTC 2014 date +%s 1404372514 # 14 seconds after "date -u" command date -u +%s 1404372515 # 15 seconds after "date -u" command
date Command in Linux
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
When we work in the Linux command-line or write shell scripts, we often need to handle date and time values, such as outputting a date in our required formats, getting a relative date-time, or changing the system date and time.
In this tutorial, we’ll take a closer look at the date utility and learn its common usages.
2. Introduction to the date Command
The date command is preinstalled by default in all Linux distros since it’s a part of the GNU coreutils package.
The syntax of using the date command is:
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
If we don’t give any options, the date command will print the current system date and time including the day of the week, month, time, year and timezone:
$ date Sat 21 Mar 2020 08:51:23 PM CET
However, if we pass different options, the date command can do more than that.
Some common usages of the date command are:
-
- display the current date and time from a given timezone
- display the date and time in a required format
- display the date and time from a given string
- get a relative date and time
- set the system date and time
Let’s go through some examples to learn how to handle each of these.
3. Override the Timezone
3.1. Display Date and Time in UTC
The date command will output the date and time in the timezone defined in our system.
UTC is a special timezone. The date command supports the option -u to display the date and time in UTC:
$ date -u Sat 21 Mar 2020 08:51:03 PM UTC
3.2. Display Date and Time in Another Timezone
The date command will by default use the timezone defined by /etc/localtime. The /etc/localtime file is usually a symbolic link, pointing to a timezone file:
$ readlink /etc/localtime /usr/share/zoneinfo/Europe/Berlin
Sometimes, we want to know the date and time in another timezone. We can set the environment variable TZ to override the timezone setting for the date command:
$ date Sat 21 Mar 2020 10:07:37 PM CET $ TZ="EST" date Sat 21 Mar 2020 04:07:42 PM EST
We can assign the TZ variable with a timezone abbreviation, such as the EST in the example above, or a name in timezone database:
$ TZ="Asia/Tokyo" date Sun 22 Mar 2020 06:07:55 AM JST
The changed environment variable TZ is only for the following date command, the system timezone retains the original value:
$ date Sat 21 Mar 2020 10:09:10 PM CET
4. Display Date and Time in Various Formats
4.1. Display Date and Time in Supported Formats
The date command supports many display formatting options. We can pass a required format to date:
Let’s see a couple of examples of changing the date format:
$ date '+Current Date: %F%nCurrent Time: %H:%M:%S' Current Date: 2020-03-21 Current Time: 22:23:03 $ date '+Today is %A, the %wth day in the week %W.%nUnix timestamp for now: %s' Today is Saturday, the 6th day in the week 11. Unix timestamp for now: 1584826306
Some commonly used formats are:
- %A – locale’s full weekday name (for example, Sunday)
- %D – date; same as %m/%d/%y
- %F – full date; like %+4Y-%m-%d
- %s – seconds since 1970-01-01 00:00:00 UTC
- %y – last two digits of the year (00..99)
- %Y – year (for example, 2020)
- %Z – alphabetic time zone abbreviation (for example, EST)
- %N – nanoseconds
We can find the full list of supported format sequences in the man page or the output of date –help.
4.2. Display Date and Time in Milliseconds
By default, the date command doesn’t support displaying a date and time in milliseconds. However, the date command supports the nanoseconds format %N. For example, we can get the nanoseconds of the current time:
If we round the nanoseconds to the first three digits, we’ll have the milliseconds:
We can combine the “%3N” format with other formats to display a date and time with milliseconds.
Let’s get the milliseconds since 1970-01-01 00:00:00 UTC:
We can also display the current date and time with milliseconds:
$ date +"%F %T.%3N" 2020-09-30 22:07:30.450
5. Display Date From a Given String
We can pass a static date or time string value together with the -d option to the date command and let it parse and print the date and time in our required format.
For example, we can pass a date string to indicate 18 Nov 1976 18:18:18 to the date command in several ways:
$ date -d'1976-11-18 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'11/18/1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'18 Nov 1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'19761118T18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'@217185498' Thu 18 Nov 1976 06:18:18 PM CET
From the examples above, we see that the date string pattern supported by the date command is pretty flexible. It can be a Unix timestamp following a @, an ISO date format, or even a human-readable date string.
6. Get Relative Date and Time
In the previous section, we’ve seen how flexible the date string for the -d option can be. In this section, we’ll continue using -d’dateString’ to let the date command calculate a relative date and time in the past or future.
In this section, all examples of relative date calculation will be based on the current date and time:
$ date Sun 22 Mar 2020 11:27:41 PM CET
6.1. yesterday and tomorrow
Let’s start with getting one day before and after the current date. It is pretty straightforward, we just pass yesterday and tomorrow with the -d option:
$ date -d'yesterday' Sat 21 Mar 2020 11:30:32 PM CET $ date -d'tomorrow' Mon 23 Mar 2020 11:30:37 PM CET
6.2. next, ago and last
Passing yesterday or tomorrow with the -d option is helpful for us to get one single day before and after the base date.
But what if we want to get an arbitrary number of days before and after? Or even in other units, such as weeks, months, years and so on?
The next, ago, and last expressions can help us with that:
$ date -d'3 days ago' Thu 19 Mar 2020 11:40:14 PM CET $ date -d'next 2 month' Thu 23 May 2020 12:40:23 AM CET $ date -d'last week' Sun 15 Mar 2020 11:40:29 PM CET $ date -d'next wed' Wed 25 Mar 2020 12:00:00 AM CET $ date -d'last Sunday' Sun 15 Mar 2020 12:00:00 AM CET
6.3. +x and -x
In addition to those human-readable date strings such as next and last, we can also pass a number with a unit to the -d option. A positive number will get a date-time in the future, while, a negative number indicates a date-time in the past:
$ date -d'+7 day' Mon 30 Mar 2020 12:54:43 AM CET $ date -d'-3 year' Wed 22 Mar 2017 11:54:51 PM CET $ date -d'10 week' Mon 01 Jun 2020 12:55:08 AM CET
This way makes it easy to calculate relative date-time programmatically, for example, in a shell script.
6.4. Get Relative Date and Time Based on a Given Date
So far, we’ve seen some examples of getting a relative date and time based on the current date-time. They’ll work for a given date string as well.
Let’s see some examples of getting relative date-time based on 1976-11-18:
$ date -d'19761118 -3 days' Mon 15 Nov 1976 12:00:00 AM CET $ date -d'19761118 last year' Tue 18 Nov 1975 12:00:00 AM CET $ date -d'19761118 tomorrow' Fri 19 Nov 1976 12:00:00 AM CET $ date -d'19761118 2 month ago' Sat 18 Sep 1976 12:00:00 AM CET
7. Change the System Time
We can use the date command with the -s option to change the system clock manually. Since changing the system time will affect current running programs, only the root user is allowed to do that change.
For example, the next command will set our system time to 1976-11-18 18:18:18 :
root# date -s "1976-11-18 18:18:18"
8. Conclusion
The date command is a very convenient utility to handle date and time values.
In this article, we’ve focused on and explored its common, practical usages.
how to get local date/time in linux terminal while server configured in UTC/different timezone?
how to get local date & time in linux terminal while server configured in UTC or different timezone? here is what I get now but I’d like to see in local timezone. For eg: PST/PDT.
[jenkins@myServer ~]$ date Thu Jul 28 18:16:48 UTC 2016
I’m not looking to change system time using hwclock or updating /etc/localtime. Just want to change it for a user. Also, please let me know — how to persist it for future logins.
3 Answers 3
Use the TZ environment variable to pass the desired timezone to date :
You can find the available timezones in the /usr/share/zoneinfo/ directory and subdirectories. For example, /usr/share/zoneinfo/America/New_York defines TZ=America/New_York .
$ date Fri Jul 29 06:31:53 BDT 2016 $ TZ='America/New_York' date Thu Jul 28 20:31:58 EDT 2016 $ TZ='America/Los_Angeles' date Thu Jul 28 17:31:54 PDT 2016
For a local time, use «date«. For UTC time, use «date -u«. Note that, if you use «date» in the server’s terminal it returns the server local time.
How did this attract the most upvotes? Not only does it not answer the actual question at hand, it suggests to use local time on the server instead. The latter being pretty silly — the convenience of not having to set the TZ in one’s local shell is fat outweighed by the fact that UTC doesn’t have the stupid issue of daylight savings times.
I agree with @tink, My host/dev box is always set at UTC so all my logs default to UTC so I can compare times across boxes in different timezones. I’m looking for a way to say based on my current internet provider what time zone am I likely in. If that doesn’t work then I’ll just have to hard-code GMT+4 and deal with daylight savings times on a manual basis.
You can show local time by overriding the TZ environment variable for the process which prints the date. POSIX says a lot about the topic, beginning with
This variable shall represent timezone information. The contents of the environment variable named TZ shall be used by the ctime() , ctime_r() , localtime() , localtime_r() strftime() , mktime() , functions, and by various utilities, to override the default timezone.
Conventional 3-character timezone values were some time ago (more or less) standardized to deprecate the 3-character forms, using the combined standard and daylight savings time form. The preferred form used for PDT is PST8PDT .
There’s a page on VMware showing the names and mentioning that they are used on Linux; you may notice that very few of those are 3-character form (aside from the generic UTC +offset).