Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic - dateformat

Status
Not open for further replies.

jpillonel

Programmer
Dec 17, 2003
61
CH
Hello,

I have a script that receive a date in parameter like these 02032006 (ddmmyyyy).

my dateparmam = $_[0]

How to convert this date in 060302 (yymmdd) ?

Thanks
 
Just use a regular expression:

Code:
my $date = '02032006';
$date =~ s/(\d\d)(\d\d)\d\d(\d\d)/$3$2$1/; # (ddmmyyyy) to (yymmdd)
print "$date";
# Outputs 060302

- Miller
 
Hello,

Thanks, works fine .. I wondering that a module was existing for converting all the dates format into anoter one.

Thanks again
 
Sorry again but if I set a second variable using the same regualr expression, the value is not correct:

$datend = 15032006;
$datend =~ s/(\d\d)(\d\d)\d\d(\d\d)/$3$2$1/; --> OK 060315
$datestart = 01012006;
$datestart =~ s/(\d\d)(\d\d)\d\d(\d\d)/$3$2$1/; --> WRONG, return always the same value: 267270.

I can set any value at $datestart variable, the returned value by the expression is always the same ?

Does anyone have an idea ?

Thanks in advance
 
Your problem is with this assignment:

Code:
$datestart = 01012006;

You do not want a number there, you want a date "string".

Code:
$datestart = '01012006';

Obviously, the number assignment works fine when given data that begins with 1-9. But preceeding a number with 0 implies that it is oct, which translates it to 267270. Try "$num = 010" as a sanity check.

- Miller
 
I'm not sure that the problem comes from the first 0.

For example if I set the same date for the two variables like this:

$datend = 15032006;
$datend =~ s/(\d\d)(\d\d)\d\d(\d\d)/$3$2$1/; --> OK 060315
$datestart = 15032006;
$datestart =~ s/(\d\d)(\d\d)\d\d(\d\d)/$3$2$1/; --> WRONG, return always the same value: 267270.

I can set any value for the second variable (which start with or without a 0), the result is always the same.

 
jpillonel said:
I'm not sure that the problem comes from the first 0.

...

I can set any value for the second variable (which start with or without a 0), the result is always the same.

I'm certain.

01012006 in oct is 267270. The regex fails to work because the new "string" is no longer 8 characters but 6.

I doubly know this is true because of the last statement you said. You can set "any" value for the second variable and it always gives you the same result? This obviously implies some type of typo in the variable names as your second variable assignment isn't being used for anything.

Add "use strict" to the beginning of your script and declare all your variables with my.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top