I encounter problem when parsing date via native JS Date object.
new Date("I'm really clever date 8745")
This expression returns valid Date which was pretty shocking for me. How to prevent this special behavior ?
EDIT: Date interprets last number as year ...
EDIT: Chrome, version (48.0.2564.116)
EDIT: Expected format is "2016-03-20T18:05:53.485Z" (JSON stringify)
if you intend to "match" any valid date (eg. christmas...), it is far from simple. if istead you want to allow only some format type, i'll go with regexp. here a simple not very extended example:
function isprobablyavaliddate(str){
var allowed = /\d{4}[\\\/-]{1}\d{2}[\\\/-]{1}\d{2}/
//example allowed date formats: yyyy-mm-dd, yyyy/mm/dd
return allowed.test(str)
}
var testString = "I'm really clever date 8745"
//catch valid string before doing anything with a date...
if ( isprobablyavaliddate(testString) ) mydate = new Date(testString)
else ...
for your valid format requested in your edit: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/ (the ^ is for match initial character)
I used regex for testing if string is valid ISO date.
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;
Works perfectly.
Related
I want to be able to get the date format string from a date string in JavaScript.
I also am using Moment.js as well if this can be achieved using this.
As an example if I have a string such as
2019-01-01 15:00:00
I'd like something like this returned
YYYY-MM-DD HH:mm:ss
I've searched the documentation but cannot find any answers to this.
You can use Parse Date Format plug-in:
This plugin extracts the format of a date/time string.
Here a live example:
const input = "2019-01-01 15:00:00"
const dateFormat = moment.parseFormat(input);
console.log(dateFormat);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://gr2m.github.io/moment-parseformat/moment-parseformat.js"></script>
Edit after #VLAZ comment, parseFormat has the preferredOrder option:
parseFormat tries to figure out the the order of day/month/year by itself if it finds 3 numbers separated by ., - or /. But if it can't, it will fallback to preferredOrder, which can either be set as an object to differentiate by separator, or as a simple string.
It doesn't seem like moment's date parsing functionality is working as I would expect it. The documentation says the following:
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
I have the following code:
moment(value, "MM/DD/YYYY");
As I'm testing, I'm entering strings that do not adhere to the MM/DD/YYYY and yet they are parsed successfully as valid moment date.
For example, 1-1-asdf resolves to 01/01/2018.
How can I specify one or more date formats that should be used for string parsing and if it does not match a specified format, the parsed moment will be invalid?
For a quick validation, you could use a regex check:
if (!/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/.test(value)) {
// Invalid date string
}
Moment has a "strict" mode for parsing dates:
moment("1-1-asdf", "MM/DD/YYYY").toString() => valid, parsed as 01/01/2018
moment("1-1-asdf", "MM/DD/YYYY", true).toString() => invalid, parsed as "Invalid date"
Refer to https://momentjs.com/docs/#/parsing/string-format/ for more info.
Im trying to create regex pattern in javascript to validate datetime format yyyy-MM-dd hh:mm:ss
/([0-2][0-9]{3})\-([0-1][0-9])\-([0-3][0-9]) ([0-5][0-9])\:([0-5][0-9])\:([0-5][0-9])(([\-\+]([0-1][0-9])\:00))/
here is an example on jsfiddle
but its not working when i test it against this date time 2017-08-31 01:22:34
can anybody help me to know whats wrong in my pattern
Thank you
It's because the pattern currently requires, rather than makes optional, the timezone modifier, which isn't present in the example date you gave.
Change the last part to:
( ([\-\+]([0-1][0-9])\:00))?
Also:
your hours sub-group is matching 0-59 rather than 0-23.
you're escaping a number of things you don't need to, e.g. : and -
the pattern allows for invalid dates e.g. 39 as a day.
Revision:
/^([0-2][0-9]{3})\-(0[1-9]|1[0-2])\-([0-2][0-9]|3[0-1]) ([0-1][0-9]|2[0-3]):([0-5][0-9])\:([0-5][0-9])( ([\-\+]([0-1][0-9])\:00))?$/
Note this will not account for invlaid dates in certain months e.g. 30th February. That means either making the pattern more complicated or using something better suited than REGEX for date validation.
It's because of the last part which should be optional (([\-\+]([0-1][0-9])\:00))?
Here is a demo
var a = /([0-2][0-9]{3})-([0-1][0-9])-([0-3][0-9]) ([0-5][0-9]):([0-5][0-9]):([0-5][0-9])(([\-\+]([0-1][0-9])\:00))?/;
console.log('2017-08-31 01:22:34'.match(a))
BTW, you don't have to escape :. - should be escaped only when used inside brackets []
In java, we can use like below and also you can take this pattern for javascript
private static Pattern DATE_PATTERN = Pattern.compile(".*?\[0-9\]{4}-(0\[1-9\]|1\[0-2\])-(0\[1-9\]|\[1-2\]\[0-9\]|3\[0-1\]) (2\[0-3\]|\[01\]\[0-9\]):\[0-5\]\[0-9\]:\[0-5\]\[0-9\]");
public void test() {
if(!DATE_PATTERN.matcher(line.trim()).matches()) {
//code here
}
}
If you want to check only date then remove *? from the pattern
I am using a JS date library which has a simple asString() formatting syntax e.g. dd mmm yyyy produces 01 Jan 1970.
Unfortunately should the month happen to contain a letter that appears in the formatting string it can go wrong, e.g. `Date('2014-09-01').asString('dd mmm yyyy') = 01 Septe9ber 2014'
To solve this is quite simple; alter the asString() method to use the format '[dd] [mmm] [yyyy]' instead. However this comes from a global format string used by other methods. The only method that needs the square brackets is the asString method.
So my ideal solution is to simply add a function in that method which replaces any of the following strings within the format string:
formats=['yyyy','yy','mmmm','mmm','mm','m','dddd','ddd','dd','d','hh','min','ss'];
With itself surrounded by []
dd/mm/yyyy => [dd]/[mm]/[yyyy]
Unfortunately the RegEx is proving to be complex - simply looping through each item results in [[d][d]]/[[m][m]]/[[yy][yy]].
So I'd like help writing this RegEx. If it can't be done please say so - I'm not interested in using new libraries as a solution but would consider solutions which solved the problem in a different way within the current asString method (i.e. no breaking changes)
This should do:
var regex = /(min|y+|m+|d+|h+|s+)/g,
newString = format.replace(regex,'[$1]');
Tested with the format "dd/mm/yyyy", resulted in "[dd]/[mm]/[yyyy]"
I am validating dates using regular expression in javascript. The regular expression I am using is
/^(((((0?[1-9])|(1\d)|(2[0-8]))\/((0?[1-9])|(1[0-2])))|((31\/((0?[13578])|(1[02])))|((29|30)\/((0?[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$/
This matches dates accurately but it would match values such as
1/1/2001ff even though I am using $ to mark the end of string.
But if I give values like ff1/1/2001 it would invalidate it. So it's considering the start of the string and ignore the end of string part.
Does anyone know the reason for this.
From: Detecting an "invalid date" Date instance in JavaScript
if ( Object.prototype.toString.call(d) === "[object Date]" ) {
// it is a date
if ( isNaN( d.getTime() ) ) { // d.valueOf() could also work
// date is not valid
}
else {
// date is valid
}
}
else {
// not a date
}
Logically, it makes much more sense to check if the date is valid rather than using a regex to match a date. However, if you're trying to search for dates, your regex still works (I tested it in Notepad++ find for example.) Other than that, like the comment said, there's no reason to have such a complicated regex.
As correctly pointed out by Dracs, the issue was with missing brackets. Thank you very much for pointing that out.
The reason for not using javascript date object is we need only to allow mm/dd/yyyy format in the textbox. So it would be easy to use a regex to validate textbox.