best date regex mm/dd/yyyy - asp.net

please help me to find the best regex for the dateformat mm/dd/yyy and m/d/yyyy.
i have tried different links but everyone has some problems. so please help me to solve this.
What is the MM/DD/YYYY regular expression and how do I use it in php?
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5
http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html

Use one of the DateTime.TryParse overloads (or DateTime.TryParseExact if you want more control) to validate whether a string is a valid representation of a DateTime.
DateTime dt;
if(DateTime.TryParse(someInputString, out dt)
{
// it is valid and dt can be used
}
This would be a better approach than regular expressions - it is faster, well tested and will produce a valid DateTime object.

If you need a regular expression then something like this would work (for most cases, since it allows 31st of February).
(0?\d|1[012])\/([012]?\d|3[01])\/\d{4}
Usually it is much better to use DateTime parsing for verifying date formats. Like DateTime.TryParseExact method. You can use the regular expression in the browser (in JavaScript) but you should really use DateTime parsing on the server side.

Related

Date validation in Ballerina

I have to validate different types of dates in ballerina. I have been trying deal with it using regex, however there are many ways to write a date (eg. 25/05/2020, 05/25/2020, 25-May-2020 etc.).
It is hard to predict all the types. What's more, it would be nice to validate whether the received input is really a date - that includes different number of days in different months or leap years. Generally such regex would be monstrous (anyway really big). Are you aware of any existing library that would provide a shortcut or have date validation?
Is there any way to facilitate such task?
Validating all the date formats at a single go is a challenge, not only in Ballerina, but in the other languages too. Ballerina do not have a single method to validate all the date formats, but you can use ballerina/time library to parse date strings to Time records.
import ballerina/time;
public function main() {
string timeString = "<your time string>";
string timeFormat = "<your time format>";
time:Time|time:Error time = time:parse(timeString, timeFormat);
}
In the sample, the time:parse() will return a valid Time record, if the provided is a valid time, according to the provided time format.
I know this is not the exact answer you want, but this is the way to parse a time in Ballerina.
Alternatively, there are some Java answers which can be used (I am not sure whether they fulfil your requirement though) with Ballerina - Java interoperability.

extract elasticsearch date from a <start-date>/<duration> XBRL-JSON format

I am storing XBRL JSON using elasticsearch.
This xBRL-JSON OIM spec describes the oim:period property:
Otherwise, an ISO 8601 time interval representing the {interval}
property, expressed in one of the following forms:
<start>/<end>
<start>/<duration>
<duration>/<end>
Where <start> and <end> are valid according to the xsd:dateTime datatype, and <duration> is valid according to xsd:duration.
Examples from arelle's plugin look like this:
2016-01-01T00:00:00/PT0S
2015-01-01T00:00:00/P1Y
I notice that arelle's plugin exclusively produces this format:
<start>/<duration>
My question
Is there a way to save at least the <start> part as a date type in elasticsearch?
Ideas I had:
elastichsearch only (my preference)
Use a custom date format which anticipates the /<duration> part, but ignores it
I haven't checked Joda yet; will it ignore characters in the date format if they aren't part of the special character? Like the "/" delimiter or the "P" which precedes any duration value (like PT0S and P1Y above)?
EDIT So the single-quote character escapes literals; this works yyyy'/P' will accept a value '2015/P'. However, the rest of the duration could be more dynamic
Re: dynamic; will Joda accept regex or wildcard character like "\d" or "+" qualifier so I can ignore all the possible variations following the P?
Use a character filter to strip out the /<duration> part before saving only <start>as datetime. But I don't know if character filters happen before saving as type: date. If they don't, the '/`part isn't stripped, and I wouldn't be passing valid date strings.
Don't use date type: Use a pattern tokenizer to split on /, and at least the two parts will be saved as separate tokens. Can't use date math, though.
Use a transformation; although it seems like this is deprecated. I read about using copy_to instead, but that seems to combine terms, and I want to break this term apart
Some sort of plugin? Maybe a plugin which will fully support this "interval" datatype described by the OIM spec... maybe a plugin which will store its separate parts...?
change my application (I prefer to use elasticsearch-only techniques if possible)
I could edit this plugin or produce my own plugin which uses exclusively <start> and <end> parts, and saves both into separate fields;
But this breaks the OIM spec, which says they should be combined in a single field
Moreover it can be awkward to express an "instant" fact (with no duration; the PT0S examples above); I guess I just use the same value for end property as start property... Not more awkward than a 0-length duration (PT0S) I guess.
Not a direct answer, but it's worth noting that the latest internal drafts of the xBRL-JSON specification have moved away from the the single-field representation. Although the "/" separated notation is an ISO standard, tool support for it appears to be extremely poor, and so the working group has chosen to switch to separate fields for start and end dates. I would expect Arelle support to follow suit in due course.

Error converting date for Calendar in asp.net

I have written code for two checkboxes:
string sdate= Convert.ToDateTime(txtFromDate.Value);
string edate=Convert.ToDateTime(txtEndDate.Value);
I am getting the following error: "String was not recognized as a valid DateTime".
Well, it's reasonably clear: the input string wasn't in an appropriate format. I suggest that instead of using Convert.ToDateTime, you use DateTime.TryParseExact and specify the format string (or strings) that you expect the user to enter.
Using TryParseExact instead of just ParseExact means you can detect if the user has entered an incorrect date without an exception being thrown - and you should check for that. Basically the return value of TryParseExact indicates success or failure, and an out parameter is used to capture the parsed date/time on success.
What I hadn't noticed to start with is that you're then trying to assign a DateTime value to a string. That's not going to work - but we can't advise you on what you should be doing instead without knowing what you want to do with the data. I suspect you want to change sdate and edate to be DateTime variables instead.

Parse string pattern into Date in Flex

Is there some way in flex to parse strings to date. I want it to support custom formats similar to 'dateformatter'. Using 'dateformatter' class we can parse date object in various string formats as specified by 'formatString property'. I want it other way round, from string to date. The parse method 'Date.parse(my_string)' does string parsing but for a very limited set of formats. Can't we have something similar to following, where user can specify his/her own formats.
someformatter.formatString = 'HH::MM::SS' ;
mydate = someformatter.formatTodate('23::56:34');
Will 'parseDateString' method of dateformatter be useul here?
Thanks in advance.
You can use
DateField.StringToDate()
This is static function of DateField class, and returns Date object.

(ASP.NET) How do I remove special characters when doing a DateTime.Now.ToString()

So I have a flashobject which I need to pass a formatted DateTime string to.
My code:
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
which outputs as: 2009-09-16 22:26:45
However when it is actually output to HTML and swfobject it renders it as:
so.addVariable("inNowDate","2009-09-16+22%3a25%3a13");
I think this is messing up a calculation that the flash object does based off the current time. Do I need to encode or decode this?
Any help would be greatly appreciated! Thanks!
It's not that you have gained special characters, but rather certain special characters you already had are now URL encoded.
There's not enough information present for me to see exactly where this URL encoding his happening. Can you post a bit more context?
When you output to html, try using UrlDecode.
http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

Resources