Date from timestamp linux

Timestamp To Date Converter

Convert timestamp to date or date to timestamp easily

Convert timestamp to date

Convert date to timestamp

How It Works

Timestamp Online is timestamp converver between unix timestamp and human readable form date. If you want to convert timestamp, it is sufficient to either enter your timestamp into input area, or you can construct URL with your timestamp — http://timestamp.online/timestamp/ .

Timestamp Online also supports countdown, so you can see, how much time remains to particular timestamp. URLs for countdowns have following form — http://timestamp.online/countdown/ .

Current Timestamp Examples

These examples are showing how to get current unix timestamp in seconds. These examples are returning timestamp in seconds, although some of the languages are returning timestamp in milliseconds.

Current Date and Time Examples

These examples are showing how to get current date and time that could be presented to the end-user.

Timestamp to Date Examples

These examples are showing how to convert timestamp — either in milliseconds or seconds to human readable form.

Parse Date to Timestamp Examples

These examples are showing how to parse date in human readable form to unix timestamp in either milliseconds or seconds.

Unix Time

Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds. It is used widely in Unix-like and many other operating systems and file formats. Because it does not handle leap seconds, it is neither a linear representation of time nor a true representation of UTC.

Источник

Get current time in seconds since the Epoch on Linux, Bash

I need something simple like date , but in seconds since 1970 instead of the current date, hours, minutes, and seconds.

date doesn’t seem to offer that option. Is there an easy way?

7 Answers 7

This should work:

Get the seconds since epoch(Jan 1 1970) for any given date(e.g Oct 21 1973).

Convert the number of seconds back to date

Читайте также:  Запуск режима восстановления mac os

The command date is pretty versatile. Another cool thing you can do with date(shamelessly copied from date —help ). Show the local time for 9AM next Friday on the west coast of the US

So far, all the answers use the external program date .

Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat , handled by strftime(3) ( man 3 strftime for informations about the formats).

So, for a pure Bash solution:

or if you need to store the result in a variable var :

No external programs and no subshells!

Since Bash 4.3, it’s even possible to not specify the -1 :

(but it might be wiser to always give the argument -1 nonetheless).

If you use -2 as argument instead of -1 , Bash will use the time the shell was started instead of the current date. This can be used to compute elapsed times

Pure bash solution

Since bash 5.0 (released on 7 Jan 2019) you can use the built-in variable EPOCHSECONDS .

There is also EPOCHREALTIME which includes fractions of seconds.

EPOCHREALTIME can be converted to micro-seconds (μs) by removing the decimal point. This might be of interest when using bash ‘s built-in arithmetic (( expression )) which can only handle integers.

In all examples from above the printed time values are equal for better readability. In reality the time values would differ since each command takes a small amount of time to be executed.

With most Awk implementations:

This is an extension to what @pellucide has done, but for Macs:

To determine the number of seconds since epoch (Jan 1 1970) for any given date (e.g. Oct 21 1973)

Please note, that for completeness, I have added the time part to the format. The reason being is that date will take whatever date part you gave it and add the current time to the value provided. For example, if you execute the above command at 4:19PM, without the ’00:00:00′ part, it will add the time automatically. Such that «Oct 21 1973» will be parsed as «Oct 21 1973 16:19:00». That may not be what you want.

Источник

Convert Unix timestamp to a date string

Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line?

date might work, except it’s rather awkward to specify each element (month, day, year, hour, etc.), and I can’t figure out how to get it to work properly. It seems like there might be an easier way — am I missing something?

Читайте также:  Уведомления экрана блокировки windows 10

11 Answers 11

With GNU’s date you can do:

On OS X, use date -r .

Alternatively, use strftime() . It’s not available directly from the shell, but you can access it via gawk. The %c specifier displays the timestamp in a locale-dependent manner.

date -d @1278999698 +’%Y-%m-%d %H:%M:%S’ Where the number behind @ is the number in seconds

This solution works with versions of date which do not support date -d @ . It does not require AWK or other commands. A Unix timestamp is the number of seconds since Jan 1, 1970, UTC so it is important to specify UTC.

If you are on a Mac, then use:

Command for getting epoch:

As @TomMcKenzie says in a comment to another answer, date -r 123456789 is arguably a more common (i.e. more widely implemented) simple solution for times given as seconds since the Unix Epoch, but unfortunately there’s no universal guaranteed portable solution.

The -d option on many types of systems means something entirely different than GNU Date’s —date extension. Sadly GNU Date doesn’t interpret -r the same as these other implementations. So unfortunately you have to know which version of date you’re using, and many older Unix date commands don’t support either option.

Even worse, POSIX date recognizes neither -d nor -r and provides no standard way in any command at all (that I know of) to format a Unix time from the command line (since POSIX Awk also lacks strftime() ). (You can’t use touch -t and ls because the former does not accept a time given as seconds since the Unix Epoch.)

Note though The One True Awk available direct from Brian Kernighan does now have the strftime() function built-in as well as a systime() function to return the current time in seconds since the Unix Epoch), so perhaps the Awk solution is the most portable.

Источник

How to convert DATE to UNIX TIMESTAMP in shell script on MacOS

On Linux you can convert a date like «2010-10-02» to a unix timestamp in shell script by

Since Mac OS does not have the equivalent -d for date. How do you go about converting a date to a unix timestamp in a shell script.

8 Answers 8

This works fine for me on OS X Lion.

man date on OSX has this example

Which I think does what you want.

You can use this for a specific date

Or use whatever format you want.

date -j -f «%Y-%m-%d» «2010-10-02» «+%s»

I used the following on Mac OSX.

Alternatively you can install GNU date like so:

  1. install Homebrew: https://brew.sh/
  2. brew install coreutils
  3. add to your bash_profile: alias date=»/usr/local/bin/gdate»
  4. date +%s 1547838127
Читайте также:  Средства windows для работы с графической информацией

Comments saying Mac has to be «different» simply reveal the commenter is ignorant of the history of UNIX. macOS is based on BSD UNIX, which is way older than Linux. Linux essentially was a copy of other UNIX systems, and Linux decided to be «different» by adopting GNU tools instead of BSD tools. GNU tools are more user friendly, but they’re not usually found on any *BSD system (just the way it is).

Really, if you spend most of your time in Linux, but have a Mac desktop, you probably want to make the Mac work like Linux. There’s no sense in trying to remember two different sets of options, or scripting for the mac’s BSD version of Bash, unless you are writing a utility that you want to run on both BSD and GNU/Linux shells.

Источник

How to convert timestamps to dates in Bash?

I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns:

Both commands should output «Wed Mar 3 13:38:49 2010».

16 Answers 16

On later versions of common Linux distributions you can use:

works for me on Mac OS X.

% date —help | grep — -r -r, —reference=FILE display the last modification time of FILE

This version is similar to chiborg’s answer, but it eliminates the need for the external tty and cat . It uses date , but could just as easily use gawk . You can change the shebang and replace the double square brackets with single ones and this will also run in sh .

You can use GNU date, for example,

You can use this simple awk script:

Since Bash 4.2 you can use printf ‘s %(datefmt)T format:

That’s nice, because it’s a shell builtin. The format for datefmt is a string accepted by strftime(3) (see man 3 strftime ). Here %c is:

%c The preferred date and time representation for the current locale.

Now if you want a script that accepts an argument and, if none is provided, reads stdin, you can proceed as:

You can get formatted date from timestamp like this

I use this when converting log files or monitoring them:

In OSX, or BSD, there’s an equivalent -r flag which apparently takes a unix timestamp. Here’s an example that runs date four times: once for the first date, to show what it is; one for the conversion to unix timestamp with %s , and finally, one which, with -r , converts what %s provides back to a string.

Источник

Оцените статью