How do I validate mm:ss format using javascript - javascript

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.

Related

Format MomentJS Date Output

Trying to format a date in an application I'm working on to:
// Example format: YYYY-MM-DDTHH:mm:ss.SSSZ
// Example: 2021-09-25T00:00:00.000Z
let date: Moment;
date.format('YYYY-MM-DDTHH:mm:ss.SSSZ');
Formatting this date variable returns the below:
2021-09-25T00:00:00.000+00:00
How can I remove those trailing zeros after the milliseconds & the plus sign? Essentially, everything after the plus sign including the plus like the below:
2021-09-25T00:00:00.000Z
Thanks in advance for the help!
You can simply remove those options ..
date.format('YYYY-MM-DD');
Or to remove anything beyond just the plus sign ..
date.format('YYYY-MM-DDTHH:mm:ss');
UPDATE
If you want the fractional of 3 IE .999 Then use three capital "S" - SSS And lose the "Z" which is your offset -- +00:00
date.format('YYYY-MM-DDTHH:mm:ss.SSS')
The documentation can be found HERE
SCREENSHOT

How to remove punctuation from time duration with Moment.js duration format

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

Regex pattern to match time like '1h 10m'

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

Javascript how to extract time from the format like 5:00 am/pm

I have a value of 12:00am in a javascript variable.
I want to to pass the time 12:00 only to database.
Please suggest me how to extract time only by excluding am/pm using javascript
Assuming you have your time string in a variable, say timeStr. You can strip off all non-sumerics and : using the following:
timeStr = timeStr.replace(/[^\d:]+/g, '');
If you just only want to remove last two chars, you can try this way:
("12:00am").slice(0, -2); // "12:00"

Date bookmarklet needs leading zero on single digit months and days

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)

Categories