Solved

getting the date

Posted on 2000-02-14
9
147 Views
Last Modified: 2010-03-05
how do i get the date, in the format m/dd/yyyy  (where the month is just one char when there is a leading zero)?

examples:
1/01/2000
2/01/2000
12/01/2000
0
Comment
Question by:dankarran
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2519911
Here's the BFI method:

$date=`date +"%m/%d/%y"`;
$date=s/^0//;

You might also look at the UnixDate module.

Rob
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2519921
Hold it!!!!

Two bugs.  Use %Y instead of %y, use $date=~s/^0//; instead of what's above.

Sorry.
0
 
LVL 3

Accepted Solution

by:
RobWMartin earned 50 total points
ID: 2519928
Sheesh, I've got to slow down.

Here's the useable answer:

$date=`date +"%m/%d/%Y"`;
$date=~s/^0//;

 



0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2519969
This may be a little bit more cross-platform friendly as NT will choke on the date command.

#!/usr/bin/perl

print getDate();

sub getDate() {
  my @timestruct = localtime(time);
  return join "/", ($timestruct[4] + 1, $timestruct[3], $timestruct[5] + 1900);
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:RobWMartin
ID: 2520038
True.  My answer is BFI, like I said.  Too lazy to look up the relevant positions in the @timestruct.  Also, I keep forgetting people actually use NT:)

I'm not sure how my original comment became a proposed answer.  I must have done it inadvertantly, so I'm going to withdraw it.  

Rob
0
 

Author Comment

by:dankarran
ID: 2520109
cheers, i didn't need code that would work on NT
0
 

Author Comment

by:dankarran
ID: 2520118
clockwatcher,

just out of interest, would the following section of code give the wrong date?

$timestruct[5] + 1900
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2520181
No, localtime returns the number of years since 1900.

See:

  perldoc -f localtime
0
 

Author Comment

by:dankarran
ID: 2520203
oh right, ok
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl rename 2 142
remove duplicates from the csv file 13 102
Edit Odbc.ini using perl module 2 78
PERL - Find newest folder 12 118
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now