LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   change one time format to other format (https://www.linuxquestions.org/questions/linux-newbie-8/change-one-time-format-to-other-format-815402/)

himu3118 06-21-2010 06:32 AM

change one time format to other format
 
hi.

the time format i have used is %m:%d:%Y:%H:%M:%S eg- 06:21:10:13:29:18 and i want to convert it to 2010-06-21 13:29:18..


thanx for any help...

acid_kewpie 06-21-2010 06:33 AM

you need to actually explaihn where you're doing this... is this in some code? What language is it etc..? We aren't psychic. All we can say is that you should change to date format string you're using and it'll print it out differently.

himu3118 06-21-2010 06:46 AM

actually i have already a file with the time format %m:%d:%Y:%H:%M:%S eg- 06:21:10:13:29:18 and i want to read the file through shell scripting and change the format to 2010-06-21 13:29:18..

MTK358 06-21-2010 07:24 AM

Maybe an AWK script like this:

Code:

BEGIN {
        FS = ":"
}

{
        print "20" $3 "-" $1 "-" $2 " " $4 ":" $5 ":" $6
}


colucix 06-21-2010 07:29 AM

Using the GNU date command you can choose the date format you want - as previously suggested. The problem is that you have to transform the input date in a format recognizable by the -d option (to specify a date other than the current one). For example you may want to change the date string into "06/21/10 13:29:18" by means of shell's parameter substitution:
Code:

$ string="06:21:10:13:29:18"
$ string=${string/://}
$ string=${string/://}
$ string=${string/:/ }
$ echo $string
06/21/10 13:29:18

then use the date command at your pleasure. In alternative you can "manually" change the date format picking up the necessary substrings and print them out in some different order, e.g.
Code:

$ d="06:21:10:13:29:18"
$ echo "20${d:6:2}-${d:0:2}-${d:3:2} ${d:9:8}"
2010-06-21 13:29:18

Edit: as you can see from MTK358's example above, there are a lot of ways to scramble a string into something else! ;)


All times are GMT -5. The time now is 03:40 PM.