473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Date Functions

1
Hi ,

I want to convert a Date in any format into any other format i required.
Is there any built in function in PERL to do the date conversion in required format.


For exampe: If date is in 28-09-2006 i want to convert it to 09-22-2006 i.e.,(dd-mm-yyyy to mm-dd-yyyy)


Mainly i want to convert date in the format 28-sep-2006 to 09-22-2006 1.e.,(dd-mm-yyyy to mon-dd-yyyy).

Means i want to know is there any built in function in PERL like to_date function in ORACLE.
Sep 28 '06 #1
2 34464
Hi ,

I want to convert a Date in any format into any other format i required.
Is there any built in function in PERL to do the date conversion in required format.


For exampe: If date is in 28-09-2006 i want to convert it to 09-22-2006 i.e.,(dd-mm-yyyy to mm-dd-yyyy)


Mainly i want to convert date in the format 28-sep-2006 to 09-22-2006 1.e.,(dd-mm-yyyy to mon-dd-yyyy).

Means i want to know is there any built in function in PERL like to_date function in ORACLE.

=========================
hi,
Use the time function to get current times in terms of seconds.
1 .$time_v = time;
Pass this result to gmtime function.
the syntax is as displayed below.
2 . ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isd st) = gmtime($time_v);

Converts a time as returned by the time function to an 9-element list with the time localized for the standard Greenwich time zone. Typically used as follows:
All list elements are numeric, and come straight out of the C `struct tm'. $sec, $min, and $hour are the seconds, minutes, and hours of the specified time. $mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December. $year is the number of years since 1900. That is, $year is 123 in year 2023. $wday is the day of the week, with 0 indicating Sunday and 3 indicating Wednesday. $yday is the day of the year, in the range 0..364 (or 0..365 in leap years). $isdst is always 0 .

Note that the $year element is not simply the last two digits of the year. If you assume it is then you create non-Y2K-compliant programs--and you wouldn't want to do that, would you?

The proper way to get a complete 4-digit year is simply:
$year += 1900;
$month=$month+1; # since months are represeneted like 0 to 11

finally
3. use sprint function to format the date according to your wish


let me know if you still faces difficulties.
Sep 28 '06 #2
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Date::Format;
  4. use Date::Parse;
  5.  
  6. foreach my $date ('28-09-2006', '28-sep-2006') {
  7.     my $newDate = $oldDate;
  8.     $newDate =~ s/^(\d{2})-(\d{2})-(\d{4})$/$2-$1-$3/; # Only include this line if you are assuming european style dates.  Ie, the day of the month number comes before the month number
  9.     my @dateArray = strptime($newDate);
  10.     $newDate = strftime("%m-%d-%Y", @dateArray);
  11.  
  12.     print "Old Date = '$date' .... New Date = '$newDate'\n";
  13. }
  14.  
  15. 1;
  16.  
  17. __END__
  18.  
The solution to your problem can be found in two CPAN modules, namely Date::Parse and Date::Format. As can be derived from their names, one is for parsing randomly formatted dates and the other is for formatting dates.

The CPAN documentation is pretty self explanatory, but unfortunately your requested example poses a problem. The Parse module assumes that numerical dates are in americanized order. Namely that the month comes before the day of month. The only way to fix this is to use a regular expression to switch the order. Obviously, you would not want to use this regular expression on any dates that were already in order, so some knowledge of the data that you are using is required in advance.
Oct 10 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ndsoumah | last post by:
Hi guys I'm having a hard time trying to validate some dates. I have a form that accept dates from users in this format (YYYY-mm-dd). I've been looking at the available datetime functions and...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Noozer | last post by:
I have a textbox on one of my forms that is used to accept a time from the user. I need to write this value to a database to trigger an event later on. What I'd like to know is... Are there...
3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
4
by: Ekong, Samuel Akpan | last post by:
Hi, I am parsing a binary file and intend to read a date(not time) field encoded thus: 0xA1290B(little-endian). Now I know the date to be 12/04/2003 but just cannot get any of the known datetime...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
9
by: Martin | last post by:
I'm retrieving some records from a database. One of the fields contains a date/time. I would like to format it as I send it out to the table in the displayed page. Can some one please tell me...
8
by: MLH | last post by:
Sometimes it works and sometimes it crashes. If I want "Today is " & Date$ & "." to appear in a query field, why might it work sometimes and not others? Would I be better to call a FN? Say,...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.