Date conversion

Hi
I want to convert MAY 05 2005 01:15:00PM date format to 2005/05/05 01:15:00PM .
CAn somebody suggest me a code ,I am new to unix shell programming.
Thanks
Arif

one way.......

echo 'MAY 05 2005 01:15:00PM' | awk -f mab.awk

mab.awk:

BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months]=i;
    delete months;
  }
}

{ printf("%s/%02d/%s %s\n",  $3, months[toupper($1)], $2, $3) }

Thanks
That was perfect .
Arif

date +%Y/%m/%d,%H:%M:%S |sed 's/,/ /g'

HI
Can we convert the date to a 24 hour format .
MAY 05 2005 01:15:00PM date format to 2005/05/05 13:15:00
Thanks Arif

BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months]=i;
    delete months;
  }
}

function conv2mil(time,   tA, tAnum, pm_am) {
  tAnum=split(time, tA, ":")
  sec=substr(tA[tAnum], 1, length(tA[tAnum])-2)
  pm_am=toupper(substr(tA[tAnum], length(tA[tAnum])-1))
  hour= (pm_am ~ /^P./) ? tA[1] + 12 : tA[1]
  return (hour ":" tA[2] ":" sec)
}

{ printf("%s/%02d/%s %s\n",  $3, months[toupper($1)], $2, conv2mil($4) ) }

Thanks
Perfect again

Arif

Hi!
..and maybe something like that: how to convert date like 2006/05/10-15:10:03 to "seconds since �00:00:00 1970-01-01 UTC'" format?

please help....
Mac

...and one more thing. It would be perfect if coded in awk.
thanx
Mac

what OS are you on?

I need it for Solaris and Ubuntu Linux

Mac

Easy in Linux, using GNU date:

date -d "2006/05/10 15:10:03" +%s

Not sure about Solaris, though.

Thanx!
Works in linux but I'm not sure about Solaris either. I'll check it tomorrow..
Mac

vgersh99- wonder if your still around? How would you alter your awk script to process the date if it appeared in the first field of a comma delimited file? i.e

JAN 03 2009 05:30:00:PM,data1,data2,data3

to

2009/01/03 17:30:00,data1,data2,data3

sorry, probably a very simple question?

BEGIN {
  FS=OFS=","
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months, " ")
  for(i=1; i<=monN; i++) {
    months[months]=i
    delete months
  }
}

function conv2mil(time,   tA, tAnum, pm_am) {
  tAnum=split(time, tA, ":")
  pm_am=toupper(substr(tA[tAnum], length(tA[tAnum])-1))
  hour= (pm_am ~ /^P./) ? tA[1] + 12 : tA[1]
  return (hour ":" tA[2] ":" tA[3])
}

{ timeAn = split($1, timeA, " ")
  $1 = sprintf("%s/%02d/%s %s",  timeA[3], months[toupper(timeA[1])], timeA[2], conv2mil(timeA[4]) )
  print
}

Thanks vgersh99, that worked great.

so sprintf can be used to assign strings to vars and in awk you can reassign new strings to the field vars.

maybe I should move this to a different forum, but I've just hit an error because one of the input lines is longer than 3000 bytes. Is there anyway of getting around this? or am i going to have to truncate the string?

If you have 'gawk', try that instead of awk/nawk.
If you're on Solaris, try /usr/xpg4/bin/awk

YMMV

I don't have gawk.

In this instance I'm not too bothered about the occasional line being corrupted so i'm just using 'cut -c1-3000' as a workaround.

Thanks again.

Hi vgersh99 - hope you're still here :slight_smile: i saw this thread and it really helped me a lot! however i'm having difficulty now figuring out how to convert this date format (May 5 13:01) to yyyymmddhhmm appending the current year (2009).

for sample: (May 5 13:01) will be transformed to 200905051301

the reason i want to have it this way is in order for me to take the time difference of two variables.

I've tried modifying your script by using sprintf, but i failed to make it work :frowning:

Hope you can help me with this :slight_smile:

Many thanks!

BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months]=i;
    delete months;
  }
}

{ gsub(":", "", $3);printf("2009%02d%02d%04d\n",  months[toupper($1)], $2, $3) }