473,394 Members | 1,956 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,394 software developers and data experts.

date format question

5
Hi all,

Hope someone can help, i get the date in the format of 12/12/2008

i 'd like to find out what day this is on, either a weekday/weekend. so output would be:
12/12/2008

midweek: fri 12 december

I'm also trying to do it the hard way, without the use of any modules .. i feel this will help my perl skills.

after the user has entered a date, i use the back tick operator to ask the shell a cal command and then put it all into an array.

I then try to see if i can change all single digits to double digits,i.e.

1 -> 01, 2 ->02...

but can't do it for the very last number for any month... whereas the other ones i can :(

Here is my attempt:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. system('clear');
  4.  
  5. my %days = (
  6.         Sun => 'Sunday',
  7.         Mon => 'Monday',
  8.         Tue => 'Tuesday',
  9.         Wed => 'Wednesday',
  10.         Thu => 'Thursday',
  11.         Fri => 'Friday',
  12.         Sat => 'Saturday',
  13.         );
  14.  
  15. my %months = (
  16.         '01' => 'January',
  17.         '02' => 'February',
  18.         '03' => 'March',
  19.         '04' => 'April',
  20.         '05' => 'May',
  21.         '06' => 'June',
  22.         '07' => 'July',
  23.         '08' => 'August',
  24.         '09' => 'September',
  25.         '10' => 'October',
  26.         '11' => 'November',
  27.         '12' => 'December',
  28.         );
  29.  
  30. print "Enter a date dd/mm/yyyy\n";
  31. chomp (my $ans = <>);
  32.  
  33. my @tmp = split(/\//,$ans);
  34.  
  35. print "day - $tmp[0], month = $tmp[1] year = $tmp[2]\n";
  36.  
  37. print "month is $months{$tmp[1]} \n";
  38.  
  39. my @caldata = `cal -m $tmp[1] $tmp[2]`;
  40.  
  41. foreach my $line (@caldata){
  42.         if ($line =~ /[a-zA-Z]+/){
  43.                 $line = "";
  44.                 next;
  45.         }
  46.         else{
  47.                 chomp($line);
  48.                 $line =~ s/ (\d[^\d]| \d$)/0$1/g;
  49.                 #$line =~ s/ (\d)[^\d]/0$1 /g;
  50.                 my @line = split(/ /,$line);
  51.                 print "$line\t element count - ", scalar @line, "\n";
  52.         }
  53. }
  54.  
the very last date on any line is giving me probs, the single digit remains single, like:

01 02 03 04 05 06 7

Any help would b very much appreciated.

thanks
g0uki
Apr 14 '08 #1
4 1775
numberwhun
3,509 Expert Mod 2GB
I haven't extensively read the page (due to not having much time at the moment), but you may want to check out the DateTime module from CPAN. It may do what you are looking for.

Regards,

Jeff
Apr 14 '08 #2
g0uki
5
Thanks Jeff,

I got it working in the end :))

the server this will live on doesn't have DateTime.pm nstalled and i'm not sure if i'll be allowed to install it. Server guys for ya!

i modded the following at line 48:

Expand|Select|Wrap|Line Numbers
  1.                 $line =~ s/ (\d[^\d])/0$1/g;
  2.                 $line =~ s/ (\d)$/0$1/g;
  3.                 $line =~ s/\s{2} /XX /g;
  4.  
and now it works like a dream.. only i'm not too happy about doing a regex 3 times in three lines.. but hey.. as i learn more i feel i'll pick up perl's short hand approach.

btw, at one point i tried alternation approach..

$line =~ s/ (\d[^\d])| (\d)$/0$1/g

which i think only works for the first remebered element?
because at one point both will be true, is there a way to harness this?

thanks
g0uki
Apr 14 '08 #3
numberwhun
3,509 Expert Mod 2GB
Glad that you got it working. Its always a good feeling when you get something working.

Don't forget though, even though you cannot install modules, it doesn't mean that you cannot dissect them and pull out of them the code that does what you want. I have had to do that before a few times. Sometimes its the only way around such "server guys" road blocks.

(when are those server guys gonna stop us from doing our job?)

Regards,

Jeff
Apr 14 '08 #4
KevinADC
4,059 Expert 2GB
There is no need to shell out to the 'cal' application. Use Time::Local. see this snippet I wrote on another forum:

http://www.daniweb.com/code/snippet570.html
Apr 14 '08 #5

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

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
6
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
10
by: ARC | last post by:
Hello all, General question for back-end database that has numerous date fields where the database will be used in regions that put the month first, and regions that do not. Should I save a...
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...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
6
by: Mtek | last post by:
Hi, We have a form where the user selects a date from a calendar, the date is in the format May 23, 2008. The date in the datebase is in the format 05212008. What we need to do is get the...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.