Gonzalo.- Gonzalo.- - 9 months ago 25
Javascript Question

Detect dates between strings and times using momentjs

I have a string like this

Occurs every 1 weeks on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday effective 2015-07-23T00:00:00 from T16:00:00 to T17:00:00


You can see I got words, times and dates. I need to apply formats to dates and times, and internationalization to strings.

The code works like this

var result = '';
// get values as an array
var arrValue = value.split(' '), resultArr = [];
// iterate in each element
for (var i = 0; i < arrValue.length; i++) {
// validate is a date
if (moment(arrValue[i], moment.ISO_8601).isValid() && (arrValue[i].indexOf('T') != -1)) {
// push into array
resultArr.push(moment(arrValue[i]).format('L'));
// validate is a time
} else if (moment('0001-01-01' + arrValue[i], moment.ISO_8601).isValid() && (arrValue[i].indexOf('T') != -1)) {
// push as a result the date
resultArr.push(moment('0001-01-01' + arrValue[i]).format('LT'));
} else {
// push the string in the collection
resultArr.push(sgI18NService.translate(['portfolio', 'activity-recurrence-field-' + arrValue[i]], arrValue[i]));
}
}


which it works fine (then I join the strings). The problem is that in IE is not working properly to recognize some strings, and it confuses them as a Date, for example, after I split the strings I got every day as a string and when I evaluate

moment('0001-01-01Thursday,', moment.ISO_8601).isValid()


I get false in Chrome (which is fine, since Thursday is a string, not a date) BUT in IE I get true. I checked and it looks like that is transforming that invalid date to minimum date

Tue Jan 01 1901 00:00:00 GMT-0500


That example was when evaluating the second if. I have this issue of getting valid dates with Thursday and with Tuesday.
What I'm doing wrong ? How can I difference strings from dates and from times in IE ?

edit: The strings are limited, I have different combination of string but they are not entered by the user.

Answer Source

Well, as my input strings were limited and I knew the case I would have, I matched the string to have numbers

/\d/.test() 

worked for me