Solved

getting the date

Posted on 2000-02-14
9
151 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

830 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