Solved

getting the date

Posted on 2000-02-14
9
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

688 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