Check if date valid using moment and format 'YYYY-MM-DDThh:mm:ss.SSS+ss:ss' does not work - momentjs

I'm trying to check if a string YYYY-MM-DDThh:mm:ss.SSS+ss:ss
But I always get false.
For example:
const value = '2022-02-16T22:23:53.000+00:00'
moment(value, DATE_FORMAT, true).isValid();
Please advise, am I using the wrong format ?

Date format should have been - YYYY-MM-DDTHH:mm:ss.SSSZ

Related

How to check if a string denotes a 24 hour valid time format using momentJS?

The code:-
moment(moment("2022-12-12").format("YYYY-MM-DD"), "YYYY-MM-DD", true).isValid()
returns true as 2022-12-12 is a valid date in YYYY-MM-DD format.
Using the same logic, I tried to check if var timeString = "16:00:00" is a valid time or not.
However, the following code:-
moment(moment("16:00:00").format("HH:mm:ss"), "HH:mm:ss", true).isValid()
always gives me false.
What am I doing wrong?
It's always giving true as you are formatting to that particular type first than checking if its valid which is why its always true.
I think you want something like this.
function isTimeFormat(time) {
return moment(time, 'HH:mm:ss', true).isValid();
}

Format local time to custom time

I have a saved time that is in the format of this: moment().format('llll').
I want to convert that time to this format: YYYYMMDD?
I tried this:
let Time = moment().format('llll')
moment(Time, 'YYYYMMMDD')
That results in moment.invalid(/* 2019年7月4日星期四 16:03 */)
Anyone an idea on how to do it?
moment().format('llll') gives you a formatted string.
To create a moment object with a formatted string, you should also provide the string format.
let Time = moment().format('llll')
moment(Time, 'llll')
After that, you can easily format moment object as string
moment(Time, 'llll').format('YYYYMMMDD')

Parse alfresco date

I'm developing a custom validator of a date input in my workflow form and I get a null after parsing a date this is what I done:
// check dates can be parsed
str_expiryDate = field.form.prop_wfbxTestWorkFlow_NfDate.value;
console.log("Non conformite"+str_expiryDate);
str_reminderDate = field.form.prop_bpm_workflowDueDate.value;
console.log("echeance"+str_reminderDate);
Alfresco.logger.warn("Expiry Date: " + str_expiryDate + " | Reminder Date: " + str_reminderDate);
d_expiryDate = Date.parse(str_expiryDate);
console.log("nfDate"+str_expiryDate);
d_reminderDate = Date.parse(str_reminderDate);
console.log("Date echéance"+d_reminderDate);
and then i get this in console:
Non conformite2013-06-21T00:00:00.000+01:00 echeance2013-06-09T00:00:00.000+01:00
nfDatenull
Date echéancenull
How I can parse these two dates and then compare it? .thanks
Use Alfresco.util.fromISO8601(date)
According to the client-api docs
Convert an ISO8601 date string into a JavaScript native Date object
You are parsing the "value" of a date, not the date itself.
The best way to compare is, imho, using the format YYYYMMDD, and than compare it as a number.
Something like this (there is sure a far more elegant way to do that, but at this time it's the only one that got me):
var indexDate=str_expiryDate.indexOf("-");
var dayDate=str_expiryDate.substring(0, 2);
var monthDate=str_expiryDate.substring(3, 5);
var yearDate=fromData.substring(6, str_expiryDate.length+1);
int dataNew=yearDate+monthDate+dayDate;
and than compare the two dates value.
Obviously check if the index value are correct, I didn't double checked them.
Hope il helps.

How do I validate a string to be a date in the format dd-M-y, and not be a past date?

I have a date field and the format is "dd-M-y", example 01-Jan-2013. First I want to check the format which must be "dd-M-y" and secondly the date shouldn't be in the past but can be today and onward.
How would I do that? I would like to use regular expressions but I don't know what a suitable one would be.
You should use DateTime.TryParseExact rather than using Regex to validate your DateTime
string testDate = "01-Jan-2013";
DateTime temp;
if (DateTime.TryParseExact(testDate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp) &&
(temp > DateTime.Today))
{
//Valid date and greater than today
}
else
{
//Invalid date or less than today
}
I think you should bind the user to fill the date in correct format instead of checking for it...
The Best solution in this case would be MaskEditExtender

xml and condition by dateTime

XML
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-04-16T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
<CalendarFairs>
Code
var elements = from element in doc.Descendants("CalendarFair")
where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now
select new
{
dateStart = element.Element("DateStart").Value,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
foreach (var item in elements)//send this error
{}
System.FormatException: The string was not recognized as a valid DateTime. There is a
unknown word starting at index 0.
why error?
Try to change it as follows:
var elements = from element in doc.Descendants("CalendarFair")
let start = element.Element("DateStart").Value
where DateTime.Parse (start)==DateTime.Now.Date
select new
{
dateStart = start,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
EDIT: based on the XML you have posted the query above works pretty well. Try to test it with this input:
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-03-20T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
</CalendarFairs>
Note that I have inserted today's start date. Actually I think the result was empty just because there weren't entries with actual date.
It sounds like one or more of your input <DateStart> strings is not in a valid DateTime format.
Can you post some sample input XML?
It may be that you need to provide the date format using ParseExact - http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

Resources