Format MomentJS Date Output - javascript

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

Related

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

Validate dateFormat in dd/mm/yyyy using regex [duplicate]

This question already has answers here:
Javascript date regex DD/MM/YYYY
(13 answers)
Closed 7 years ago.
I want to validate the format of the date value entered by a user using regex with javascript.
My regex doesn't allow the '/' character , /[^0-9\.]/g,''
But I want to let '/' pass the regex test too. What modification do I need to make here?
Modified from this answer you can be pretty exact with this. This works for the years 1000-9999, is Proleptic Gregorian and assumes that we won't change how leap-years work until the year 9999 ;)
^(?:(?:(?:0[1-9]|1\d|2[0-8])/(?:0[1-9]|1[0-2])|(?:29|30)/(?:0[13-9]|1[0-2])|31/(?:0[13578]|1[02]))/[1-9]\d{3}|29/02/(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00))$
Debuggex Demo
"20/11/1992".match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)
The above snippet should do, but there are too many validations to be performed on dates, so I wouldn't recommend regex.
Instead, I'd say do it like most websites do and place 3 combo boxes (dd/mm/yyyy), and allow the user to select a date, then you validate that date using the Date() constructor (if the values haven't changed, the date is correct).
note: the answer is based upon the assumption that you don't want to use any of the existing libraries (or the native validation provided by browser when using input[type="date"])
You can use this regex:
/(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/
This validates the date with format dd/mm/yyyy and also checks for leap years.
It depends on how strict you need to be? I thing that simple:
/[0-3]\d\W[01]\d\W(?>19|20)\d{2}/g
should be sufficient.
day: [0-3]\d 2 digits, first 0-3, second any number \d
month: [01]\d 2 digits, first 0 or 1, second any number
year: (?>19|20)\d{2} 4 digits, starts with 19 or 20 (for 19th and 20th century) and next any digit two times {2}
Also note, I used \W to match single non-word character as workaround to match /. Are you sure that you cannot use escaped slash \/ instead?

regExp code to compare a date format like this: DD-MM-YYYY HH-MM-SS

I have the following piece of javascript to compare a DD-MM-YYYY HH-MM-SS date format, but for some reason it doesn't work. Can anyone see what is wrong with it? (mind that there is a whitespace between the date & time.
new RegExp (/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/)
The actual data I'm matching to this regExp looks like any of the following:
1-1-2013 0:00:00
1/1/2013 0:00:00
01-01-2013 00:00:00
01/01/2013 00:00:00
31-12-2013 23:59:59
These are the max ranges. Note that from 0:00:00 till 9:59:59 the time is noted like that and for 10:00:00 it's 6 digits, preferably it would handle leap years too.
It looks like the delimiter for the time is : and not -. Try:
new RegExp (/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})\s([0-1][0-9]|[2][0-3])\-([0-5][0-9])\-([0-5][0-9])$/)
You should keep things small and test little pieces of regex.
You want to allow one-digit for an hour, but you've forgotten to place it in your regex. It should look like that:
([0-9]|[0-1][0-9]|[2][0-3])
Now it works:
regex = new RegExp (/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})\s([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/)
match = '1-1-2013 0:00:00'.match(regex)
console.log(match)

What regular expression should I use to match this date pattern: DAY.month.YEAR?

I have this date format:
DAY.month.YEAR (today: 28.06.2011)
I will need a Regular Expression (RegEx) pattern for matching this date format.
Can anyone post a solution for this problem?
Derived from http://www.regular-expressions.info/dates.html:
(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d
This matches a date in dd.mm.yyyy format from between 01.01.1900 and 31.12.2099. It will, however, still match invalid dates, because validating leap years, for example, can not be done with regex (at least not very easily).
However, a regex is probably unnecessary. Javascript example:
var date = "28.06.2011".split("."); // split up the date by the dots
// parse the components into integers
var day = parseInt(date[0]);
var month = parseInt(date[1]);
var year = parseInt(date[2]);
// if you want the date in a date object, which will fix leap years (e.g. 31.02 becomes 03.03
var date = new Date(year, month - 1, day);
Note that when creating a date object, month starts at zero.
Which method you use depends on what you need this for. If you want to find dates in a text, use the regex. If you simply want to parse the date into a date object, use the second method. Some extra validation is possibly necessary to make sure the date is valid, as the javascript Date object does not care about February having 31 days, it simply wraps over to 3. of March.
If you wish to match the format of the date, than it will be enough to use:
\d\d\.\d\d\.\d\d\d\d
However, as David Hall pointed out in his comment to your question, you will still need to validate the date in your code. Doing this in a regex isn't easy, as you can see from Harpyon answer which - still making a "preliminary check that filter out many wrong possiblities" - also accepts 31.02.2011 as a valid date and misses out on the French revolution (14 July 1789).

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