Skip to content
Advertisement

moment.js returns invalid Date for moment (String, string)

Wants to compare the timestamp. Tried lots of ways but it is throwing an Invalid Date error. The variables startDate /endDate/testDate returns a same timestamp format in 'DD.MM.YYYY HH:MM:ss' i.e. '21.12.2021 08:29:00'

var startDate = cy.get('[data-placeholder="Select time range (from)"]').invoke('val')
var endDate = cy.get('[data-placeholder="Select time range (to)"]').invoke('val')
var testDate  = cy.get('td:nth-child(5)>span').invoke('text')

1. moment(startDate ,'DD.MM.YYYY HH:MM:ss' ).format('DD.MM.YYYY HH:MM:ss')  ///  returns Invalid Date 
2.  moment(startDate ,'DD.MM.YYYY HH:MM:ss' ).format('DD.MM.YYYY HH:MM:ss').valueOf() ///  returns Invalid Date
3. moment(startDate ,'DD.MM.YYYY HH:MM:ss' ).format()  ///  returns Invalid Date 
4. moment(startDate ,'DD.MM.YYYY HH:MM:ss' ) ///  returns Invalid Date 

Also tried using isBetween() function. But for all the conditions it is throwing result as false.

       cy.log( moment(testDate , 'DD.MM.YYYY HH:MM:ss').isBetween(
            (moment(startDate,'DD.MM.YYYY HH:MM:ss')), (moment(endDate,'DD.MM.YYYY HH:MM:ss'))
            ) )

please correct me.

Advertisement

Answer

From https://momentjs.com/docs/#/parsing/string-format/

momentjs docs screenshot

MM in HH:MM:ss should be small letters. That is, moment('21.12.2021 08:29:00', 'DD.MM.YYYY HH:mm:ss') should work fine.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement