Question : Convert a text date to unix date format for importing to DB

This occurs in my raw data: '8/31/2009 12:00:00 AM'

I want simply 2009-08-31 and ignore the time part

In my text editor I can use grep find-and-replace to 2009-8-31:
(\d\d?)/(\d\d?)/(\d\d\d\d) 12:00:00 AM,
\3-\2-\1
but how do I get a leading zero to occur when needed. Maybe this calls for sed?

Answer : Convert a text date to unix date format for importing to DB

if you want year, month, day format,
1:
2:
3:
$date = '8/31/2009 12:00:00 AM';
$date =~ s/^(\d+)\/(\d+)\/(\d+).*/sprintf("%04d",$3)."-".sprintf("%02d",$1)."-".sprintf("%02d",$2)/e;
print "$date\n";
Open in New Window Select All
Random Solutions  
 
programming4us programming4us