Solved

getting the date

Posted on 2000-02-14
9
148 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting to linux server using Net::Telnet module 25 232
compress files in RAR using perl 13 80
Awk Question 2 127
what are these modules in perl script 4 98
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

805 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