I have an ISO date which is stored as a string. I want to get only the hours part as is. The below code does not work. So, how do I make it work using dayjs?
function getHours(date:string){
const hour : dayjs(date).hour();
console.log(hour);
}
getHours("2023-05-06T08:00:00Z");//Expected 8, but got 1.
2
Answers
In
dayjs
you need to use the utc plugin:You have a string with a specific format and you want a substring. Using a date library like
dayjs
or even built-inDate
would be overkill. Get the substring and optionally convert it to a number:No plugins, no external libraries and only one conversion from string to number required.