Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Time and date format regular expression
Old 09-08-2006, 10:35 AM Time and date format regular expression
Average Talker

Posts: 16
Trades: 0
Hi,

I am going to input a long string,which prompts a user for a time and date in the format
Code:
  hh:mm mm/dd/yyyy
It is valid only two digit for hour, month, date.
It is invalid to use one digit for hour such as 1:30 10/03/2005
Code:
    str= prompt("Enter the time and date in the format\n" + "hh:mm mm/dd/yyyy",
                                                                                              "00:00 01/01/2000");
how to write the regular expression.

Thank in advance!

zhshqzyc
zhshqzyc is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-08-2006, 09:30 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
This should work:
Code:
var str = prompt("Enter the time and date in the format\n" + "hh:mm mm/dd/yyyy","00:00 01/01/2000");

if (str.match(/^\d\d:\d\d[ ]\d\d\/\d\d\/\d{4}$/) != null) {
	alert("Format Correct");
} else {
	alert("Invalid Format");
}
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-09-2006, 10:08 AM Re: Time and date format regular expression
Average Talker

Posts: 16
Trades: 0
thanks funkdaddu!

if the format is correct, how to output string?

I mean
Code:
document.writeln("please supply missing fragment");
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-09-2006, 01:28 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
The string is str:

document.writeln(str);
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-09-2006, 03:49 PM Re: Time and date format regular expression
Average Talker

Posts: 16
Trades: 0
yes, however



str is str = prompt( "Enter the time and date in the format\n" +
"hh:mm mm/dd/yyyy",
"00:00 01/01/2000" );
how can I only to display the time and data part?

have a good day!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-09-2006, 11:51 PM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
You can spilt the str string on the space into an array:

str.split(" ")[0]; //will give you the time
str.split(" ")[1]; //will give you the date
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-10-2006, 09:50 PM Re: Time and date format regular expression
Average Talker

Posts: 16
Trades: 0
Hi, your answer is pretty great. But what is wrong with my expression?

Code:
reg = /^\d{2}:\d{2} \d{2}\/\\d{2}\/\\d{4}$/
I am a novice. I spent a lot time, still could not figure out,

the one more question is how to seperate the string into five part.

such as

RegExp.$1 = ...// hour
RegExp.$2 = ...// minute
RegExp.$3 = ...// month
RegExp.$4 = ...// day
RegExp.$5 = ...// year

It needs to remove the special characters such ":","/".

Thanks!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Old 09-11-2006, 12:18 AM Re: Time and date format regular expression
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
You just need to put each bit you want in between parentheses, and you don't need the double //:

Code:
reg = /^(\d{2}):(\d{2}) (\d{2})\/(\d{2})\/(\d{4})$/
then you can use the RegExp.$1 for the hour, RegExp.$2 for the minutes, RegExp.$3 for the Month... etc...
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-11-2006, 08:58 AM Re: Time and date format regular expression
Average Talker

Posts: 16
Trades: 0
I appreciate your assistance.

God bless you!
zhshqzyc is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Time and date format regular expression
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30432 seconds with 12 queries