As you know the format is like:
"YYYY-MM-DDTHH:NN:SS.ZZZ+XX:XX" (i.e. "2009-03-24T16:24:32.057+01:00")
I have to do it in a ActionScript3 class but any source will be appreciated, thanks.
\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d.\d{3}\+\d\d:\d\d
Or something similar?
More checks of the valid range are probably better done after the reg.ex.
Java regex, tested to read a date like "2001-12-15T07:30:40Z":
"^\\d{4}-[0-1][0-3]-[0-3]\\d{1}T[0-2]\\d{1}:[0-5]\\d{1}:[0-5]\\d{1}Z$"
Have a look at this related question:
Actionscript 3 - Fastest way to parse yyyy-mm-dd hh:mm:ss to a Date object?
The accepted answer provides a way to parse an UTC time string to a Date object.
Following Regex is perfect for any UTC date formats
"^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?$"
Ref: https://www.regexpal.com/94925
Regex should be something like this:
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{2}:\d{2}
Related
I write a regular expression to determine the date time.(the assumption are every month has 31 days and the year only contain 1900 to 2099)
^(((((0?[1-9]|1[012])[- /.\\](0?[1-9]|[12][0-9]|3[01]))|((0?[1-9]|(1|2)[0-9]|3[01])[- /.\\](0?[1-9]|[1][012])))([- /.\\](19|20)\d{2})))$
the format of date time are:
dd-mm-yyyy
mm-dd-yyyy
0m-0d-yyyy
0d-0m-yyyy
m-d-yyyy
d-m-yyyy
everything works fine except one thing; if the date time like 32-10-2010, in my thought it should not be recognized, but in regex tester 2-10-2010 has been recognized. I wonder if there is any way to modify the regular expression to prevent it.
After removing the / at the end, your RegEx is working for me. Here's a simple Sublime Text RegEx Find/Replace:
Here is the adjusted regex:
^(((((0?[1-9]|1[012])[- /.\\](0?[1-9]|[12][0-9]|3[01]))|((0?[1-9]|(1|2)[0-9]|3[01])[- /.\\](0?[1-9]|[1][012])))([- /.\\](19|20)\d{2})))$
But a better solution would be to use the languages native date functionality. I can't think of a language that doesn't have inbuilt methods for these sorts of things.
For example, using JavaScript's Date object, or some such...
Try this one:
^((3[01]|0?[1-9]|[1-2][0-9])-(1[012]|0?[1-9])|((1[012]|0?[1-9])-(3[01]|0?[1-9]|[1-2][0-9])))-(19|20)[0-9][0-9]$
I've already given such an answer here.
This match one invalid date : 29-02-1900 but is correct for any date between 01-01-1900 and 31-12-2099
The valid date format pattern in your case is:
/^\d{1,2}-\d{1,2}-\d{4}$/
With RegEx you can validate only format of date, not a correct date, because it's a bad practice! Months can be with different days, so good luck to write pattern that will be consider it.
If You want to validate is date correct, use other build-in functions in your language. For example checkdate for PHP or etc.
I am getting one data filed from one text file at format 'MM/DD/YY'(like '08/13/13'). Now I want to convert it as 'DD-MM(in words)-YYYY' like (13-SEC-2013).
Please help me in this conversion.
Please use this function.
TO_CHAR(TO_DATE(DATE,'MM-DD-YY'),'DD-MON-YYYY')
Or
v_PORT(DateTime)-TO_DATE(TO_CHAR(INPUTPORT),'MM/DD/YY')
o_PORT(String)--TO_CHAR(v_PORT,'DD-MON-YYYY')
Both it's working and coming like this.(13-SEC-2013)
Convert input value to date by using to_char(to_date(in_date, 'mm/dd/yyyy'), 'DD-MON-YYYY')
Credit: Change Date Format
V_PORT=TO_CHAR(TO_DATE(INPUT_PORT,'YYYY-MM-DD'),'DD/MM/YYYY')
NOTE:- IF input port data is in timestamp then you have to use substr function to get only the date.
you can use like SUBSTR(INPUT_PORT,1,10)
All close
TO_CHAR(TO_DATE(I_DATE,'MM/DD/YY'),'DD-MON-YYYY')
Try this it will work
IIF(ISNULL(HIREDATE),TO_DATE(TO_CHAR(SYSDATE,'MM-DD-YY'),'DD-MON-YYYY'),HIREDATE)
I need an expression which matches,DateTime format (DD/MM/YYYY),i've already found it.
However,it only works to (1/6/2009) or (1/5/2010),it doenst support (01/06/2009) or (01/05/2010).
How can i check if a string is a dateTime in Javascript?
You can check this nifty library : Date.js
Try this (which I found here):
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
In order to make this match dates without leading zeros on the month and the day you will need to change it up a bit:
(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
How about using a datejs library? It has no problem with those patterns.
I have this regex (\d{4})-(\d{2})-(\d{2}) to detect a valid date, however, it is not perfect as some of the incoming data are 2009-24-09 (YYYY-DD-MM) and some are 2009-09-24 (YYYY-MM-DD).
Is it possible to have a one-line regex to detect whether the second & third portion is greater than 12 to better validate the date?
If you don't know the format, you will get ambiguous results.
take 2010-01-04 is that January 4th or March 1st?
You can't validate that with a regex.
As Albert said, try to parse the date, and make sure users know which format to use. You might try to separate the month and year portions into different fields or comboboxes.
Regex are not really good with dates validation, in my opinion is better to try to parse the date, and you could keep the regex as a sanity check before parsing it.
But if you still need it you can fix the month section using the following regex (\d{4})-(\d{2})-((1[012])|(0\d)|\d) but it goes downhill after that, since you need to check for correct days on months and leap years.
(\d{4})-((0[1-9]|1[0-2])-(\d{2}))|((\d{2})-(0[1-9]|1[0-2]))
YYYY-(MM-DD)|(DD-MM)
to validate YYYY-MM-DD or YYYY-DD-MM:
$ptn = '/(\d{4})-(?:(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-2])|(0[1-9]|' .
'[1-2][0-9]|3[0-2])-(0[1-9]|1[0-2]))/';
echo preg_match_all($ptn, '2009-24-09 2009-09-24 dd', $m); // returns 2
even so, the date could be invalid, e.g.: 2010-02-29, to deal with that there's checkdate():
checkdate(2, 29, 2010); // returns false
I get travel confirmations that look like this:
"SQ 966 E 27JUL SINCGK"
= "Airline Space Flight Space BookingClass Space Date_with_Month_as_name Space 3LetterFrom 2LetterTo".
I can chop all this into pieces using a regex to submit it to a website. But the site would expect instead of 27JUL 27/07/2009 or at least 27/07. Is there a way to transform a regex result based on a piece in the input. Jan -> 01, Feb -> 02 ... Dec -> 12.
(Regex flavour is Java)
DateFormat is a more appropriate class:
DateFormat output = new SimpleDateFormat("dd/MM", Locale.US);
DateFormat input = new SimpleDateFormat("dd MMM", Locale.US);
System.out.println(output.format(input.parse("24 Dec")));
output:
24/12
In Perl syntax (s{pattern}{replacement}):
s{([0-9][0-9])JAN}{\1/01}
s{([0-9][0-9])FEB}{\1/02}
s{([0-9][0-9])MAR}{\1/03}
s{([0-9][0-9])APR}{\1/04}
s{([0-9][0-9])MAY}{\1/05}
s{([0-9][0-9])JUN}{\1/06}
s{([0-9][0-9])JUL}{\1/07}
s{([0-9][0-9])AUG}{\1/08}
s{([0-9][0-9])SEP}{\1/09}
s{([0-9][0-9])OCT}{\1/10}
s{([0-9][0-9])NOV}{\1/11}
s{([0-9][0-9])DEC}{\1/12}
(Yes this is long and ugly, but it would probably work).
I would be very careful with doing this with regular expressions as they don't tell you how the conversion went.
Extract every bit of information manually. Sanity check everything, and then use the SimpleDateFormat parser to get a Date object you can use from there on.
It isnt a regex solution, but you could use SimpleDateFormat to help you with your final formatting. You should note in the JavaDoc that this is not a thread-safe option out of the box.
Alternatively, you could use DateFormatSymbols.getShortMonths() and iterate over the months to identify the index* and format your string manually.
*dont forget to add 1 ;)
edit:
I am not sure what you are looking for is possible in Java regex without the ablility to make code changes. The conditional constructs that Perl supports are not supported by Java because Java provides if-then-else support as a language feature.