Solved

getting the date

Posted on 2000-02-14
9
146 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

18 Experts available now in Live!

Get 1:1 Help Now