I'm not really good at handling datetime. I used momentjs for a ionic app for manipulating time but I want to achieve something that I couldn't.
I used a pipe for that and I want to display based on if how many days have been passed or if weeks and months or years. Using relative time would help me that like the fromNow()
method and the calendar()
of momentjs. But in my case I would have multiple conditions
.
Here is the sample code below of my pipe
transform(value: Date | moment.Moment, dateFormat: string): any {
if (moment(value) < moment(value).subtract(7, 'days')) {
return moment(value).format('llll') // Use this format if weeks, months or years has passed
} else if (moment(value) < moment(value).subtract(1, 'days')) {
return moment(value).calendar(); // Use calendar time if 1 day has passed
} else {
return moment(value).fromNow(); // Use relative time if within 24 hours
}
}
If seconds, minutes or hours has passed until 24 hours I will use the fromNow()
method but when days passed I will use the calendar()
and if weeks, months or years passed use this format('llll')
.
Can someone shed some light for me?
Thanks in advance.