convertion of seconds to date and time in linux shell script
Hi,
I am looking for command or function to convert seconds to date. In shell script i will get seconds into one varible, I need to convert seconds to date and append to file(Ex: filename.<date-time-format>) I.e ts=1280353895 -->This is date and time in seconds.This needs to convert YYYYMMDDHHMMSS format. Through date command with options i can get this format. #date +%Y%m%d%H%M%S ; date +%s; date 20100813061446 --->YYYYMMDDHHMMSS format.(I am looking this format) 1281705286 ----> time in seconds(getting into variable.need to convert) Fri Aug 13 06:14:46 PDT 2010---->date Can any one help to get this format? Thanks in advance. |
Hi,
Are you looking for this: date -d @$ts Or formatted differently: date -d @$ts +"%d-%m-%Y %T %z" Hope this helps. |
Try this
Code:
date --date=@1280353895 |
druuna you beat me :)
@ OP as your require format you can use Code:
date --date=$ts +"+%Y%m%d%H%M%S" |
Hi sem/druuna,
Thanks for your immediate response to my query. I got the required format and i used following command. date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S" [ts=1280353895] O/P:20100728225135 [YYYYMMDDHHMMSS] Thanks Naveen |
Hi,
Nice to see you got a working solution! The 1970-01-01 part is not needed. This date is a standard default on unix/linux systems. You do not need to mention it in your command. This will do the same as the one you posted: date -d @$ts +"%Y%m%d%H%M%S" BTW: You're welcome :) |
Druuna,
you are correct The 1970-01-01 part is default linux/unix system time. with date -d @$ts +"%Y%m%d%H%M%S" this command i am getting following message date: invalid date `@1280353895' In my script i am assigning this format to one variable(DATE=`date -d @$ts +"%Y%m%d%H%M%S"`) and using this "DATE" in different places. But if i use the epoch time (DATE=`date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S"`) i am getting the the required format. Please correct me if i am wrong. |
Hi,
To put the output of a command into a variable you need to do the following: DATE=$(date -d @$ts +"%Y%m%d%H%M%S") or DATE=`date -d @$ts +"%Y%m%d%H%M%S"` -> those are back ticks. Now you can use $DATE. Hope this helps. |
Quote:
|
Hi,
I have to agree with konsolebox on this issue. What does date --version tell you? Hope this helps. |
Hi druuna,
Following is the date version. #date --version date (coreutils) 5.2.1 Written by David MacKenzie. Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. with following commands i am getting date: invalid date `@1280353895' DATE=$(date -d @$ts +"%Y%m%d%H%M%S") or DATE=`date -d @$ts +"%Y%m%d%H%M%S"` I am not sure why you are using @$ts?If i am not wrong $ts is enough. But its working fine with following command DATE=`date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S"` Thanks Naveen |
Hi,
Your date version is a bit old and that could be the problem. I have to use the @ in order to make it work (tried with the oldest date version I could find): Code:
$ date --version Hope this helps. |
Thanks a lot druuna.
I am using date -d "1970-01-01 $ts sec" +"%Y%m%d%H%M%S" in my script. Thanks Naveen |
You're welcome :)
|
All times are GMT -5. The time now is 09:05 PM. |