473,795 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date format question

5 New Member
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 1788
numberwhun
3,509 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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
43018
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 mediate between webapps and arbitrary database backends using JDBC. I am very unwilling indeed to write special-case code for particular databases. Our code has worked satisfactorily with many databases, including many instances MS SQLServer 2000...
4
3453
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 to select today. example: var dtToday = Date() if(document.frmSoftware.txtDDate.value == dtToday) { alert("You cannot select same day distributions. Please enter a new
6
31186
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 format exception (SQLSTATE=22007). The same queries worked fine on all the other workstations. The date format we want to use is "dd/mm/yyyy".After reinstalling several times the db2 runtime client w/ different options, we found on a technical forum...
20
35638
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 project the date format in vb.code ( not in Windows) and how can I find out which date format the PC Windows is using. Thanks for any response
2
11515
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: CAST(O.MYDATE AS CHAR(30)) When directly updating date fields in the main table, the logged value gets saved in the format YYYY-MM-DD as expected.
10
5824
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 date format in the table design, such as: mm/dd/yyyy? What I've done for years is to store the date format in date fields, then on the forms, based on their region, I would set the date formats on form_load
9
2943
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 how I can/should do that? Or possibly point me to an on-line explanation? Thanks ps: if it makes any difference, the data is coming from a MS Access
5
3447
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 call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page. Here is the date validation code. // date validation using SimpleDateFormat // it will take a...
6
236
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 starting and ending date of the week for the date selected in the form: May 23, 2008, and then get the records from the database where the dates fall within that range.
4
3060
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 the function using the intermediate window, it works fine. However, when I pass the dates from the code attached to my form, the results are inaccurate. You will notice my dates are in Australian format. Everything works fine using the Australian...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.