Are problems in achieving find the separator location in javascript - javascript

I have a question.
I was wondering how I can find the separator location.
Or have the date in the format DD / MM / YYYY without having to force the slash.
Any suggestions?
Thanks

You can use indexOf or lastIndexOf to find your seperator locations. If it's just a matter of formatting the date then take a look at some of the date libraries available out there.
Here are just a few:
moments.js
dates.js
xdate.js
there are many more, just G* search "javascript date library"

I was wondering if there is any function or method that I return the system date in the format:
10/12/2012 or 12/10/2012 or last 10:12:2012
but not manually enter the slash, colon or dash.

Related

datetime Regular Expressions pattern to match yyyy-MM-dd hh:mm:ss format

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

Parse String to get "date", "time" and "other", with Regexp

In Google Apps Script, I am trying to parse a string with regexp. My string looks something like this:
The first part is text only but of varying length and then follows a date (dd.mm.yyyy) and a starting time (on the European 24h scale) and an ending time (also on the European 24h scale), i.e.:
Event in Cologne dd.mm.yyyy 18:30 - 23:00
What i would like to do is parse this string with regexp in Google Apps Script in four parts and save those as new variables:
title,
date,
start time &
end time
Can anyone here help me with this?
It may help you to experiment with your regexp. If you search for "regexp tester" you'll find many available options. For example, on #gskinner's RegExr.com there is a wealth of resources; tutorials, examples, a full cheat sheet, etc. But best, a live "lab" to try them out.
So there's an example that will match the date portion of your source string. The event title comes before that, the time after... so even without a full matching regexp you should be easily able to break it down.
/([\d]+\.[\d]+\.[\d]+)/g matches date in MM.DD.YYYY or DD.MM.YYYY format (demo)
/((([0-1]?[0-9])|([2][0-3])):)([0-5][0-9])/g matches time (demo)

RegEx to split formatting string in JavaScript

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]"

Date.js parseExact with french culture

I'm using the last build of date-fr-FR.js in the svn trunk (rev 191).
The parsing seems to fail on days and months names.
Date.parse("9 3 2012")
is ok, but:
Date.parse("vendredi 9 mars 2012")
returns null.
parseExact doesn't help either:
Date.parseExact("vendredi 9 mars 2012", "dddd d MMMM yyyy")
returns null.
Anyone faced a similar issue ? Is there a more recent version of the localized files ?
Maybe you could recommend me another javascript date library if nobody can find a solution.
The French culture file fr-FR:js appears to have a few of bugs. For example the regular expression for Friday shows:
/^ve(n(.(dredi)?)?)?/i
This means than either "ve" or "ven" or "ven." or "ven.dredi" are recognized as Friday but not "vendredi". More precisely the above regex matches the "vend" and leaves "redi" unmatched, thus failing the parser. The same bug is present for all days of the week and most months.
To fix this you could replace the above regular expression with:
/^ve(n(\.|(dredi)?)?)?/i
Adding the alternate "|" after the any character ".". I have also escaped the dot because it should not match "any" character but just the dot though this would not fail your test case.

How to parse arbitrary dates with datejs parseExact

I want to parse a date in this format '11h04 31/05/2011' and other arbitrary formats using datejs, I tried:
Date.parseExact("11h04 31/05/2011", "HH'h'mm dd/MM/yyyy")
and
Date.parseExact("11h04 31/05/2011", "HH\hmm dd/MM/yyyy")
without success.
Does anyone knows the exact format/specification of parseExact or how to parse this (and others) arbitrary formats?
In java i would do this:
SimpleDateFormat sdf = new SimpleDateFormat("HH'h'mm dd/MM/yyyy");
sdf.parse(date);
P.S.: yeah, I know I can use a regex to split the string and then parse, but I want to know how to parse it with parseExact.
Thanks in advance.
Does anyone knows the exact format/specification of parseExact
Here is a link to the date.js format specifiers used by date.js and parseExact
http://code.google.com/p/datejs/wiki/FormatSpecifiers#CUSTOM_DATE_AND_TIME_FORMAT_SPECIFIERS
I can't find any way to trick the parseExact method into thinking the formatString doesn't contain a literal h between HH and mm. Tried \u0068, \0x68, no luck.
I think your best bet is
var date = "11h04 31/05/2011";
Date.parseExact(date.replace("h",""),"HHmm dd/MM/yyyy")

Categories