Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

javascript find matching mask for a date time format

how do I find the date format mask or regex from looking at a date string?

for example, if I see 02/04/2009, I want to match it with MM/dd/yyyy, or whatever would be equivalent in javascript.

I need to do this for the many varieties of date time formats: 2/3/2009 10:00:00 AM, etc. but using standard javascript format, if that exists.
0
bhomass
Asked:
bhomass
  • 10
  • 5
1 Solution
 
HonorGodCommented:
The problem is that you can't tell if 1/2/2009 is Jan 2nd, or Feb 1st...
0
 
HonorGodCommented:
if you don't have to "guess" at the MM/DD vs. DD/MM then this might help:

if ( /^\d{1,2}[-/]\d{1,2}[-/]\d{4}$/.test( val ) {
 ...
}
0
 
alien109Commented:
what are you trying to do, validate the date?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bhomassAuthor Commented:
sounds like the question was not clear.

I want to have a list of possible outcomes, consisting of date formats in javascript.

I am only going to accept month before date as in m/d/yyyy. 2/1/2009 means Feb 1st, 2009.

in HonerGod's second reply, is that suppose to return a true or false? that's not what I need. I need to distinguish between the possible outcomes of

MM/dd/yyyy, M/d/yyyy, MM/dd/yy, MM/dd/yyyy hh:mm:ss a, etc. from one input date string.
0
 
bhomassAuthor Commented:
anyone care to try. I am giving a lot of points for this.
0
 
HonorGodCommented:
Please define what you mean when you say:

"I need to distinguish between the possible outcomes of..."

What, exactly does this mean?
0
 
HonorGodCommented:
The code snippet in http://#a24198486 just shows one way to find a line of text that contains a pattern matching the specified regular expression (i.e., 1, or 2 digits, followed by either a hyphen or a slash, followed by 1 or 2 digits, followed by a hyphen or a slash followed by 4 digits)

As you noted, there are many different "date" formats, used by different browsers, and different scripts used to format date and time information to programmer taste.


0
 
bhomassAuthor Commented:
what I want to do is mimic the capability of Excel.

you know how you can enter a string like 02/02/2009 10:15:00 AM, and Excel automatically recognize it is a date time format, and even chooses the right format in the format cell dialog?

I know there are many date time formats available, but if I get a starter on the most common formats, I think I can figure out the missing ones later. if we just start with the few I laid out.
MM/dd/yyyy, M/d/yyyy, MM/dd/yy, MM/dd/yyyy hh:mm:ss

so I understand the formula you gave is for testing for all patterns with one regex? would you mind giving me 4 regex for the above four formats?
0
 
HonorGodCommented:
not at all.  This will match all of the patterns above...
if ( /^\d{1,2}\/\d{1,2}\/(\d{2}|\d{4})( \d{2}:\d{2}:\d{2})?$/.test( val ) {
  alert( 'Valid datestamp: ' + val )
}

Open in new window

0
 
HonorGodCommented:
For example, the output from the following script (with extra spaces) is:

1/2/03                          true
03/04/09                      true
Apr 1 2009                   false
04/22/2009 12:34:56  true
junk                             false

<html>
<body>
 
<script type="text/javascript">
function dateCheck( val ) {
  return /^\d{1,2}\/\d{1,2}\/(\d{2}|\d{4})( \d{2}:\d{2}:\d{2})?$/.test( val )
}
 
var data = '1/2/03,03/04/09,Apr 1 2009,04/22/2009 12:34:56,junk'.split( ',' )
 
for ( var i = 0; i < data.length; i++ ) {
  var val = data[ i ]
  document.write( val + ' ' + dateCheck( val ) + '<br>' )
}
 
</script>
 
</body>
</html>

Open in new window

0
 
bhomassAuthor Commented:
well again. that is NOT what I want, to match them all.

I want to match one at a time. that is what I mean by "distinguish"
0
 
HonorGodCommented:
Ah, please forgive me.

#1: MM/dd/yyyy

if ( /^\d{2}\/\d{2}\/\d{4}$/.test( val ) ) {
   alert( 'valid MM/dd/yyyy: ' + val )
}

#2: M/d/yyyy

if ( /^\d\/\d\/\d{4}$/.test( val ) ) {
   alert( 'valid M/d/yyyy: ' + val )
}

#3: MM/dd/yy

if ( /^\d{2}\/\d{2}\/\d{2}$/.test( val ) ) {
   alert( 'valid MM/dd/yy: ' + val )
}

#4: MM/dd/yyyy hh:mm:ss

if ( /^\d{2}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/.test( val ) ) {
   alert( 'valid MM/dd/yyyy hh:mm:ss: ' + val )
}

0
 
HonorGodCommented:
oops, typo in #4

#4: MM/dd/yyyy hh:mm:ss

if ( /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}$/.test( val ) ) {
   alert( 'valid MM/dd/yyyy hh:mm:ss: ' + val )
}

0
 
bhomassAuthor Commented:
this is great stuff. what is here all worked. please help with one last format

how to detect AM or PM at the end of
MM/dd/yyyy hh:mm:ss ?

including am/pm.
0
 
HonorGodCommented:
#5 MM/dd/yyyy hh:mm:ss AM or PM

if ( /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2} [ap]m$/i.test( val ) ) {
   alert( 'valid MM/dd/yyyy hh:mm:ss: ' + val )
}

Note, it has a space ahead of the AM or PM which much be at the end of the string.

If you don't want that space, change it to:

if ( /^\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}[ap]m$/i.test( val ) ) {
   alert( 'valid MM/dd/yyyy hh:mm:ss: ' + val )
}

0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now