473,788 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex.Validate Date and Time

Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
Jun 27 '08 #1
5 13848


"shapper" <md*****@gmail. comwrote in message
news:c9******** *************** ***********@c58 g2000hsc.google groups.com...
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
\d\d-\d\d-\d{4} \d\d:\d\d:\d\d

Matches for me....although I would probably use the DateTime.TryPar se method
instead so you don't have to limit the input to a single format.

HTH,
Mythran
Jun 27 '08 #2
On Jun 19, 2:38*am, shapper <mdmo...@gmail. comwrote:
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
Instead of RegEx, Try using DateTime.TryPar se, that should be the
better approach.

-Cnu
Jun 27 '08 #3
shapper wrote:
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4
On Jun 19, 9:47*am, Göran Andersson <gu...@guffa.co mwrote:
shapper wrote:
Hello,
What is the Regex expression to validate a date time format as
follows:
dd-mm-yyyy hh:mm:ss
An example:
20-10-2008 10:32:45
There should be a space between date and time.
Thanks,
Miguel

If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____http://www.guffa.com
Hi,

I was able to do it using:

^([0-9]{4})-([0-1][0-9])-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5]
[0-9]):([0-5][0-9])$

But this does no accept empty values.

Can I create a RegEx that accepts empty values and when the value is
not empty then it validates?

Thanks,
Miguel
Jun 27 '08 #5
shapper wrote:
On Jun 19, 9:47 am, Göran Andersson <gu...@guffa.co mwrote:
>shapper wrote:
>>Hello,
What is the Regex expression to validate a date time format as
follows:
dd-mm-yyyy hh:mm:ss
An example:
20-10-2008 10:32:45
There should be a space between date and time.
Thanks,
Miguel
If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____http://www.guffa.com

Hi,

I was able to do it using:

^([0-9]{4})-([0-1][0-9])-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5]
[0-9]):([0-5][0-9])$
That would accept a date like 2008-19-39...

This will get you a bit closer:

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])\s([0-1]\d|2[0-3])(:[0-5]\d){2}$

But still it has no idea about the different number of days in months. A
date like 2008-02-31 is still accepted.
But this does no accept empty values.

Can I create a RegEx that accepts empty values and when the value is
not empty then it validates?
You can just add ()? around the expression to make it conditional:

^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])\s([0-1]\d|2[0-3])(:[0-5]\d){2})?$

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1939
by: J. J. Cale | last post by:
Little astrology program has 2 text input boxes for birth dates. There are 3 selects for day month year that will supply output to whichever text input had the focus last. If the user picks from the list boxes the relative text input field is formatted d/m/y, the format that the cgi is expecting. The user can also input directly to the text inputs and I would like to route whatever the user inputs through a filter using regexes wherever...
1
1727
by: Colin Reid | last post by:
Hey MS, here's an apparent problem in the base class library. I pulled the email validation pattern "^((*)*@(()+(*)*\.)+{2,9})" from http://regexlib.com. If I validate the email address "test@someverylongemailaddress.com" against it by just creating a RegEx and calling IsMatch it works fine, but if I create a schema defining a simple type restricting an xs:string by the regex pattern, it takes over full minute at 100% cpu to match....
2
3428
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string was splitted. I am fascinated to the powerfull use of this RegEx class, so I wonder if it could go a step further. As a question, can regex be used to valid a set of different functions ? Example : Suppose I have to verify the correctness of an...
8
1863
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex regxUserName = new Regex(@"(?<=User-Name = )\""(+)\""", RegexOptions.None);
4
1365
by: | last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular expression used in a regex validator that works on the client side in Firefox but not in IE. Any ideas? IE always reports the field is invalid. The expression is: ^(?!\d)(?=.*\d)(?=.*)(?=.*)(?=.*).{8,25}$ If I enter "Test_Field1" Firefox considers it valid on client side, IE doesnt. Server side considers it valid too because when I submit the form in
11
3113
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
4
2798
by: ad | last post by:
I am useing VS2005 to develop wep application. I use a RegularExpress both in RegularExpressionValidator and Regex class to validate a value. The RegularExpress is 20|\-9|\-1|?\d{1} When I enter 33 and validate with RegularExpressionValidator, it fail to pass. But when I validate with regex class : Regex.IsMatch(Sight0L, @"20|\-9|\-1|?\d{1}");
6
4211
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most helpful. thanks phil
6
2074
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test it is called a 100 times but in my real app it could be called 50000 or more times. So my question is if it is somehow possible to speed this up and if so how
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
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...
1
10118
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
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
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.