Converting time format as Integer to seconds

Hello,

How can we convert date like format 20181004171050 in seconds ?

I can able to convert till date but failing for HHMMSS.

date -d "20181004" "+%s" output as 1538596800 .
But when i add hhmmss it is failing date -d "20181004172000" "+%s" result Invalid date

Kindly guide.

Regards

date -d "20181004 17:20:00" "+%s"
1538666400

works. Is that string stored in a variable? A file?

1 Like

Hello Rudic, it is in a file as one of the column " 20181004173050 "

Example:

20181004172111
20181004172113
20181004172113
20181004172114
20181004172153
20181004171906
20181004171220
20181004172157
20181004172157
20181004172157

And, how is that extracted and presented to date ?

1 Like

Can we convert it into seconds by any means ?

It is taking from db concatenating and then storing in a file like Integer format.

By any means, yes. Just insert a space a position 9, and colons at positions 11 and 13.

Dear RudiC, Kindly guide how can i implement that ?

Yes, of course - if you answer my question.

1 Like

Dear RudiC,

It is taking from database and then storing in a file like Integer format.
its like date and time are concatenating and then pushing into flatfiles.

Regards,

Try - if your shell provides "process substitution" -

date -f<(sed -r 's/([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})/\1 \2:\3:\4/' file)  "+%s"
1538666471
1538666473
1538666473
.
.
.

or

sed -r 's/([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})/\1 \2:\3:\4/' file | date -f- "+%s"
1 Like

Thanks RudiC,

One more help please, how can we set the above command for the value instead of file like 20181007130640

Regards,

I don't understand. Please rephrase.

 sed -r 's/([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})/\1 \2:\3:\4/' file | date -f- "+%s" 

like for

sed -r 's/([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})/\1 \2:\3:\4/' 20181007130940 | date -f- "+%s"

Kindly tell us what operating system and shell you're using. (Which, as you know, you should tell us every time you start a thread in the Shell Programming and Scripting forum.)

With a POSIX conforming shell, you can easily use various variable expansions to get parts of a string and assignment to assign the reformatted data into another variable. With a csh derived shell, your options might be more limited.

With awk , you can easily use substr() and print or printf or sprintf (depending on what you want to do after that).

What have you tried to solve this problem on your own?

1 Like

So - now the value is in a variable, not a file? Your request becomes confusing.

1 Like
#!/bin/bash
#  partition date string 
#  20181004172000 ->
#  0000 00 00 01 11 11
#  1234 56 78 90 12 34   position 1 -15 bash uses zero index, so pos 1 = 0 for bash
#  ---- -- -- -- -- --
#  2018 10 04 17 20 00  parititioned string
#   %Y  %m %s %H %M %s  time format spec you are implying
 
 string="20181004172000"   # variable to split
 # position index is zero based
 dt="${string:0:4} ${string:4:2} ${string:6:2} ${string:8:2} ${string:10:2} ${string:12:2}"
 echo "$dt"
 

I have to admit that your request is more than fuzzy. I am using bash by inference from the type of date command you seem to have -> implies bash may be on your system. If you cannot give details as Don and Rudi have asked for we cannot give you good answers.

Thank you RudiC, requirement fulfilled.