Linux convert date to timestamp

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

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.

OP asks about converting a specific given date to a timestamp. date +%s will just use the current date.

man date on OSX has this example

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s" 

Which I think does what you want.

You can use this for a specific date

date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s" 

Or use whatever format you want.

ON OSX Terminal using bash shell I am unable to use the above command Ex: currDate= date +%Y%m%d date -j -f «%a %b %d %T %Z %Y» «$» «+%s»

@Jagdeep This’d work on OSX only if the Timezone specified is that of system’s timezone. I am in PDT so in the above command, changing EDT wtih PDT worked for me.

I used the following on Mac OSX.

currDate=`date +%Y%m%d` epochDate=$(date -j -f "%Y%m%d" "$" "+%s") 

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

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.

Источник

Convert Date string in timestamp

Where is the fault? It seems that the variable DATEJ does not give the same as the string in the 1st line. If I don’t write EDT , the error is same but mention +%s .

Читайте также:  Ps4 pro установка linux

what’s the point of the echo -n ? why not just DATEJ=’2012-03-02 22:00′ ? Also, you need double-quotes when you use $DATEJ — e.g. date -d «$DATEJ EDT» +%s , otherwise date will get three arguments $DATEJ , EDT , and +%s instead of two. that’s why it’s complaining about the extra argument (i don’t speak or read french but i can make an uneducated guess 🙂

2 Answers 2

The problem here seems to be with the single-quotes in $DATEJ . Your variable imposes them.

DATEJ=`echo -n "2012-03-02 22:00"` 
$ date -d '2012-03-02 22:00 EDT' +%s 1330740000 $ DATEJ=`echo -n "2012-03-02 22:00"` $ echo $DATEJ 2012-03-02 22:00 $ date -d "$DATEJ EDT" +%s 1330740000 

EDIT

Actually, you don’t need to echo the date, unless your script imposes so, so the variable $DATEJ can simply be put as

Hopefully the following will help you or someone else looking for solution.

The date string can be of format mm/dd/yyyy hh:mm:ss zzz or any equivalent. For ex: «06/10/2021 21:00:00 EST» or «06/10/2021 09:00:00 EST» TZ data is optional.

You can give the following command to get epoch timestamp:

date -d «06/10/2021 00:00:00 EST» +»%s»

you can pass the string in a variable, for ex:

date_string="06/10/2021 00:00:00 EST" date -d "$date_string" +"%s" 

For your specific problem, following should work:

date -d '2012-03-02 22:00 EDT' +%s DATEJ=`echo -n "2012-03-02 22:00"` echo $DATEJ date -d "$DATEJ EDT" +%s 

Источник

How to display Unix time in the timestamp format?

srand without a value uses the current timestamp with these Awk implementations:

The following will convert Date Time to Unix time on Unix-like environment.

# Current UNIXTIME unixtime() < datetime2unixtime "$(date -u +'%Y-%m-%d %H:%M:%S')" ># From DateTime(%Y-%m-%d %H:%M:%S)to UNIXTIME datetime2unixtime() < set -- "$" "$" set -- "$" "$" "$" "$" set -- "$1" "$" "$" "$3" "$" "$" set -- "$1" "$" "$" "$" "$" "$" [ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" set -- $(( (365*$1)+($1/4)-($1/100)+($1/400) )) "$2" "$3" "$4" "$5" "$6" set -- "$1" $(( (306*($2+1)/10)-428 )) "$3" "$4" "$5" "$6" set -- $(( ($1+$2+$3-719163)*86400+$4*3600+$5*60+$6 )) echo "$1" > # From UNIXTIME to DateTime format(%Y-%m-%d %H:%M:%S) unixtime2datetime() < set -- $(( $1%86400 )) $(( $1/86400+719468 )) 146097 36524 1461 set -- "$1" "$2" $(( $2-(($2+2+3*$2/$3)/$5)+($2-$2/$3)/$4-(($2+1)/$3) )) set -- "$1" "$2" $(( $3/365 )) set -- "$@" $(( $2-( (365*$3)+($3/4)-($3/100)+($3/400) ) )) set -- "$@" $(( ($4-($4+20)/50)/30 )) set -- "$@" $(( 12*$3+$5+2 )) set -- "$1" $(( $6/12 )) $(( $6%12+1 )) $(( $4-(30*$5+3*($5+4)/5-2)+1 )) set -- "$2" "$3" "$4" $(( $1/3600 )) $(( $1%3600 )) set -- "$1" "$2" "$3" "$4" $(( $5/60 )) $(( $5%60 )) printf "%04d-%02d-%02d %02d:%02d:%02d\n" "$@" ># Examples unixtime # => Current UNIXTIME date +%s # Linux command datetime2unixtime "2020-07-01 09:03:13" # => 1593594193 date -u +%s --date "2020-07-01 09:03:13" # Linux command unixtime2datetime "1593594193" # => 2020-07-01 09:03:13 date -u --date @1593594193 +"%Y-%m-%d %H:%M:%S" # Linux command 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Альт линукс посмотреть версию

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43536

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Convert date time string to epoch in Bash

The date time string is in the following format: 06/12/2012 07:21:22. How can I convert it to UNIX timestamp or epoch?

6 Answers 6

What you’re looking for is date —date=’06/12/2012 07:21:22′ +»%s» . Keep in mind that this assumes you’re using GNU coreutils, as both —date and the %s format string are GNU extensions. POSIX doesn’t specify either of those, so there is no portable way of making such conversion even on POSIX compliant systems.

Consult the appropriate manual page for other versions of date .

Note: bash —date and -d option expects the date in US or ISO8601 format, i.e. mm/dd/yyyy or yyyy-mm-dd , not in UK, EU, or any other format.

Note: if you want to specify the time in another timezone (like UTC) add -HHMM or +HHMM to the end. So date —date=’06/12/2012 07:21:22 -0000′ +»%s» converts UTC date to unix time stamp

For GNU date, if you wish to use UTC note that the format is yyyy-mm-dd hh:mm:ss have a look at gnu.org/software/coreutils/manual/html_node/… : gdate —date=’2019-06-18 00:02:00 +0000′ +%s

For Linux, run this command:

For macOS, run this command:

date -jf "%Y-%m-%d %H:%M:%S" "1970-01-01 00:00:00" +%s 

On OSX the timestamp increases somehow with the current time. I currently don’t have an explanation for that.

Update: That was because it adjusts the timestamp for my localtime I guess. Adding a -u flag should fix that.

@AbdulRehmanJanjua The -u flag should come before the -f flag, or else the shell interprets it the format string. So, it should be: date -j -u -f «%a. «

A lot of these answers overly complicated and also missing how to use variables. This is how you would do it more simply on standard Linux system (as previously mentioned the date command would have to be adjusted for Mac Users) :

#!/bin/bash orig="Apr 28 07:50:01" epoch=$(date -d "$" +"%s") epoch_to_date=$(date -d @$epoch +%Y%m%d_%H%M%S) echo "RESULTS:" echo "original = $orig" echo "epoch conv = $epoch" echo "epoch to human readable time stamp = $epoch_to_date" 
RESULTS: original = Apr 28 07:50:01 epoch conv = 1524916201 epoch to human readable time stamp = 20180428_075001 
# -- Converts from human to epoch or epoch to human, specifically "Apr 28 07:50:01" human. # typeset now=$(date +"%s") # typeset now_human_date=$(convert_cron_time "human" "$now") function convert_cron_time() < case "$" in epoch) # human to epoch (eg. "Apr 28 07:50:01" to 1524916201) echo $(date -d "$" +"%s") ;; human) # epoch to human (eg. 1524916201 to "Apr 28 07:50:01") echo $(date -d "@$" +"%b %d %H:%M:%S") ;; esac > 

Источник

Читайте также:  Astra linux dmraid true

How to convert date to unix timestamp in shell script on macos in Linux?

Linux: how to convert DATE to UNIX TIMESTAMP in shell script on MacOS?

The Unix timestamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix timestamp is a way to represent the date and time in a single, numerical format.

There are a few ways you can convert a date to a Unix timestamp in a shell script on MacOS. Here are three methods you can use:

Method 1: Using the date command

  • Step 1 — First, open up a terminal window on your Mac.
  • Step 2 — To convert a date to a Unix timestamp using the date command, you can use the following syntax:
date -j -f "%Y-%m-%d %H:%M:%S" "YYYY-MM-DD HH:MM:SS" +%s
date -j -f "%Y-%m-%d %H:%M:%S" "2022-01-01 00:00:00" +%s

This will output the Unix timestamp for the date «2022-01-01 00:00:00».

To do this, you can use the following syntax:

This will output the date corresponding to the Unix timestamp 1609459200.

Method 2: Using the perl command

  • Step 1 — First, open up a terminal window on your Mac.
  • Step 2 — To convert a date to a Unix timestamp using the perl command, you can use the following syntax:
perl -e 'print str2time("YYYY-MM-DD HH:MM:SS")'
perl -e 'print str2time("2022-01-01 00:00:00")'

This will output the Unix timestamp for the date «2022-01-01 00:00:00».

To do this, you can use the following syntax:

perl -e 'print scalar localtime(TIMESTAMP)'
perl -e 'print scalar localtime(1609459200)'

This will output the date corresponding to the Unix timestamp 1609459200.

Method 3: Using the gawk command

  • Step 1 — First, open up a terminal window on your Mac.
  • Step 2 — To convert a date to a Unix timestamp using the gawk command, you can use the following syntax:

This will output the Unix timestamp for the date «2022-01-01 00:00:00».

a Unix timestamp to a date. To do this, you can use the following syntax:

This will output the date corresponding to the Unix timestamp 1609459200.

Conclusion

In summary, there are several ways you can convert a date to a Unix timestamp or vice versa in a shell script on MacOS. These methods include using the date, perl, and gawk commands, each with their own specific syntax. It’s important to note that the date and time must be in a specific format for these commands to work properly.

I hope these examples and explanations have been helpful and that you now have a better understanding of how to convert dates to Unix timestamps and vice versa in a shell script on MacOS. Let me know if you have any further questions or need more clarification on any of the concepts discussed here. Good luck with your development journey!

Источник

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