0

I have a string '2015_11_01_00_00' I want this convert into epoch time. I have tried the below code, but could not found it worth.

#!/bin/bash echo "mohit" date_var="2015_11_01_00_00" echo $date_var arr=$(echo $date_var | tr "_" "\n") IFS='_ ' read -r -a arr <<< "$date_var" #echo $arr echo ${arr[0]} echo ${arr[1]} echo ${arr[2]} echo ${arr[3]} echo ${arr[4]} updated_date="${arr[0]}"-"${arr[1]}"-"${arr[2]}"-"${arr[3]}"-"${arr[4]}" echo $updated_date TMRW_DATE=`date --date="$updated_date" +"%s"` echo "$TMRW_DATE" 
1
  • Have you considered time zones, ambiguity and information loss? It might not be important in your case but epoch 1459154813 can only mean one thing whereas 2015_11_01_00_00 is ambiguous without a time zone. Commented Mar 28, 2016 at 8:51

3 Answers 3

2

This one should do the trick:

#!/bin/bash echo "mohit" date_var="2015_11_01_00_00" updated_date=`echo ${date_var} | sed -e 's,_,/,;s,_,/,;s,_, ,;s/_/:/'` echo ${updated_date} TMRW_DATE=`date -d "$updated_date" +"%s"` echo "$TMRW_DATE" 
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting mohit mohit date: illegal option -- - usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] May be some environment specific issue,
In my machine, I can execute both answers correctly. Yet the other answer should modify $4-$5 to $4:$5 to get the correct result. It seems that your date utility does not accept --date option. Try to use -d instead. (Answer updated).
@Ribin Hue thanks for the help, it worked for me, even my code also worked. It was environment issue, it worked on different instance.
2

Here is one way to do it, using GNU date syntax:

#!/bin/bash echo "mohit" date_var="2015_11_01_00_00" TMRW_DATE=$( set -- $(echo $date_var | tr "_" " ") date --date="$1-$2-$3 $4:$5" +"%s" ) echo "$TMRW_DATE" 

and this should work with OS X date implementation :

#!/bin/bash echo "mohit" date_var="2015_11_01_00_00" TMRW_DATE=$(date -j -f "%Y_%m_%d_%H_%M" "$date_var" +%s) echo "$TMRW_DATE" 

3 Comments

Please note: date --date="$1-$2-$3 $4-$5" +"%s" is different from date --date="$1-$2-$3 $4:$5" +"%s" unless your are in the timezone +0:00. The later one should be the correct answer. You can see the difference when you omit +"%s".
@jlliagre I have executed your code, I m getting mohit mohit date: illegal option -- - usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] Is it some environment specific error, Im executing in on Mac machine.
@RobinHsu Thanks for pointing the issue. GNU based version corrected.
0

i was trying to do the same for postgres sql by following which produce error

SELECT *, DATEDIFF(ss, '1970-01-01T00:00:00.000', punch_time) as EpochSeconds FROM attendance 

but following code did work for me

SELECT EXTRACT(EPOCH FROM punch_time) as EpochTime FROM attendance; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.