I'm trying to build a time tracking application which needs to parse a string which can have (but not always) a time in it in one of many formats or combinations. The formats I'm checking for are
h|hr|hrs|hours
m|min|mins|minutes
These can be with a space in-between the number and the hour or minutes and can be combined or just one or the other. Some examples:
1h
1 hour 20 mins
2hrs 15 m
The regex for matching the times that I have currently:
((\d+(\.\d+)?)\s*(h|hr|hrs|hours))?(\s*(\d+)\s*(m|min|mins|minutes))?
This works fine if I just pass it the time string without anything before it. My problem is that I want to parse a full text string with the time appearing anywhere in it. Some examples:
The is a time entry for some work 1h 15m
2h 45mins This is a time entry for some work
This is a time entry 1hour 25 mins for some work
This is a time entry for some work
Does anyone have any suggestions on how to tackle this?
All you really need to do with yours is make the plural s optional, and add some word boundary tokens.
Try:
\b((\d+(\.\d+)?)\s*(h|hr|hrs?|hours?))?(\s*(\d+)\s*(m|min|mins?|minutes?))?\b
Related
I am using moment duration format to calculate total time duration it works fine, however when time duration goes in 4 digits it adds comma in hours (consider money format).
What I have:
moment.duration(33869100, 'seconds').format('hh:mm:ss', {trim: false})
Out put: 9,408:05:00 ---> note the hours have comma I need this format 9408:05:00 without comma no money format.
You can simply disable grouping like so
moment.duration(33869100, 'seconds').format('hh:mm:ss', {trim: false, useGrouping: false})
I'm not sure Moment can change that for you but you can, simply do a replace:
/* using regex, .replace(/,/g, '') replaces all commas in case you run into large numbers */
moment.duration(33869100, 'seconds').format('hh:mm:ss', {trim: false}).replace(/,/g, '')
Here's a fiddle showing it working
EDIT: refer to answer from #George, Moment can do this for you
I am automating a scenario in testcomplete tool using Javascript
So I have to verify a time format in mm:ss format) Eg:- (20:55)
But at one condition, the time will be displayed in 'mmm:ss' format.In a scenario time 120 minute is displayed as 120:00 format. That also I have validate. So in short I have to validate both format
mm:ss & mmm:ss .
You can use a regular expression to validate it.
var time = '120:50';
if (time.match(/\d+:[0-5]\d/)) {
// time is valid
}
This will match any number of minutes, but will only match a valid number of seconds.
Is there a standard javascript function to split a string into two lines of the same (or almost the same lines) without breaking the words.
What I actually want is to:
-leave the text with no changes, if the text contains less than let's say 50 symbols (including spaces),
-otherwise, split it into two lines of the same (almost the same) length.
That is needed to display the text in a good manner, to look nicely.
Examples:
Today is Monday.
Today is Monday, tomorrow is Tuesday. //less than 50 symbols.
Today is Monday, tomorrow is
Tuesday, after tomorrow is Wednesday. //splitted into two lines, Tuesday is on the second line.
one simple example uses split to find the "middle", though it's not the same "middle" as your example. you can shift the split a few slots to the left using (s.length/2)-6 if you want the first line to usually be shorter push comnes to shove. you might even consider trying (s.length*0.45) to get a little bit to the left; play around and find something that works best for your text.
here's how to find the position of a space near the middle:
var s="Today is Monday, tomorrow is Tuesday, after tomorrow is Wednesday.";
var p=s.slice(s.length/2).split(" ").slice(1).join(" ").length;
s.slice(0, s.length-p) + "\n" + s.slice(s.length-p);
/* == "Today is Monday, tomorrow is Tuesday,
after tomorrow is Wednesday." */
edit: keep in mind that "hello".slice( 2.5 ) works in JS.
I'm trying to check for a valid time value it can be anything between 00 hours 00 Mins and 00 secs and 23 hours 59min and 59 secs.
I want it to check that it matches exactly HH:MM:SS with 0 being used if there no information for part not sure if I am explain this well so an example would be.
If the time was 23 mins and 5 secs it would be
00:23:05 and reject 23:05
The expression I think is right is
^(?:(?:([01]\d|2[0-3]):)([0-5]\d):)([0-5]\d)$
I am using jQuery but as far as I know there no easy way to validate time.
Am I correct?
That looks okay to me, but if you're just validating, it would be easier not to use grouping constructs when you don't need to, which would leave something like:
^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$
That only uses grouping for the alternation operator, which makes it much easier to read, IMO.
I have a bookmarklet I use to check daily log files. However the bookmarklet I use only delivers the month and day in single digits, however the log files use double digits.
For example my bookmarklet delivers:
http://url/log/2009-5-4_localcontrol-story.log,
while the log file actually lives at:
http://url/log/2009-05-04_localcontrol-story.log
Below is my current code:
javascript:d=new%20Date();window.open("http://url/log/"+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+(d.getDate())+"_localcontrol-story.log",%20"_self");
Can you tell me an adaptation to this so I get my month and date in 2 digit format with the leading zero if necessary?
it's kind of a pain, but what I've done is to do stuff like this:
("0"+d.getDate()).slice(-2)
(add a leading zero, and slice(-2) takes the last 2 characters)