Home > Mobile >  How to check if date string contains hours/min/seconds part?
How to check if date string contains hours/min/seconds part?

Time:05-09

I have JS string with date: 21-05-2022. It is also possible to receive date string as 21-05-2022T23:15:56Z - date string with date and time data. I would like to check if incoming string contains only date or date and time. Now I am using a function which splits string by 'T' symbol and checks if resulted array has second [1] member. Maybe there is more elegant way to perform this operation? I can use moment.js library in this context.

CodePudding user response:

since your date is always string, i would do :
let result = dateString.includes('T'); if the string includes the T it means it is datetime

CodePudding user response:

Alt variant: let isTimeHas = /:/.test(dateString)

  • Related