Convert to Integer

Hi fellows!!

i'm doing something which is not working out for me properly which i don't understand why

nowdate=`date +%s`
echo $nowdate

now the problem how to convert a date which is stored in a variable

mydate="22/Oct/2011"
mydate=`date -d '$mydate' +%s`

it gives error "invalid date"

Regards

Ewa

mydate="22/Oct/2011"
mydate=`date -d "$mydate" +%s`

Hi Vgersh99 !

mydate="22/Oct/2011"
mydate=`date -d "$mydate" +%s`

Output

Replace the slashes with spaces.

mydate="22/Oct/2011"
mydate_converted=`echo "$mydate" | sed 's/\// /g'`
mydate=`date -d "$mydate_converted" +%s`
1 Like

Hi Guys !

how to convert this one ...

Date_Time="01/Jan/2011:12:55:21"
Date_Time=`date -d "$time" +%s`
echo Date_time

Output
date: invalid date `01 Jan 2011:12:55:21'

do i have to start a new thread or can anyone answer this one?

Regards,

Ewa

You may want to have a look at that thread :

see post #3

try replacing ':' as as well as '/'

mydate_converted=`echo "$mydate" |  sed 's#[/:]# #g'
1 Like

GNU date will accept epoch seconds in -d if you give it as -d @151235252...

The @ syntax is buried deeply in the info page and not mentioned in the man page.

Hi !

i already tried to remove the / and :

time="01/Jan/2011:12:55:21"
time=`echo "$time" |sed 's/\// /g'`
time=`echo "$time" |sed 's:\:: :g'`
time=`date -d "$time" +%s`

is there any simple way to convert it?
i'm using Unix Borne Shell (terminal)

and thanks to Vgersh99 to showing me the simple way of removing / and :

Regards Ewa

"What kind of shell do you have?"
"Bourne"

"What model car do you have?"
"Blue"

Bourne is a very vague answer. Code that will work in all bourne shells is often far from the most efficient possible in your shell.

That aside, I think you've overcomplicating this. Since you have date -d, you must have GNU date, which will accept dates in epoch seconds but you must put a @ before it to tell it it's epoch seconds.

$ VAR=$(date +%s)
$ echo $VAR
1320260040
$ date -d "@${VAR}"
Wed Nov  2 12:54:00 CST 2011
$

Hi !

my problem was to convert the mentioned time stamp into second with a specific format because the file i'm reading has that specific format so i'm bounded

"01/Nov/2011:03:14:59"

i tried different ways but it is not working so is it possible to convert it into seconds

as i convereted "01/Nov/2011"

date=`01/Nov/2011`
date=`echo "$date" |sed 's/\// /g'`
date=`date -d "$date" +%s`
echo $date
output
13456940....

if it is not possible then it is okay, but i guess it should be very easy for you guys

anyways thanks

Regards

Ewa

Apologies for not noticing the updated question, I was answering your OP...

You still haven't told us what shell you use. It would be a lot easier to answer if you told us what shell you use. As is, I'll write two different answers for you.

After a lot of experimentation, I've found GNU date accepts dates like these:

"01-Jan-2011 12:55:21"

So:

DATE="01/Jan/2011:12:55:21"
IFS=":/" read DD MMM YYYY HH MM SS <<<"$DATE"
date -d "$DD-$MMM-$YYYY $HH:$MM:$SS"

Or a version that will work in any generic bourne shell:

DATE="01/Jan/2011:12:55:21"
OIFS="$IFS"
IFS=":/"
set -- $DATE # Variable MUST NOT be in quotes!
IFS="$OIFS"

date -d "$1-$2-$3 $4:$5:$6"

Note that the generic version overwrites your $1,$2,... parameters.

1 Like