I want to display time in 3 different formats, along side follow the translation strategy as well. That is if user selects to translate the page, user should be able to see the date in his/her local translated with selected time format method.
- To display date in ISO format =>
2010-01-19T14:32:35+03:00
- To display Date along with Time zone =>
January 31, 2010, 14:32 (UTC+3:30)
- To display date in respect to when the record is created like =>
5 hours ago, 2 minutes ago
So I have this method in my JS file of LWC to update these date time format.
dateFormat(){
if(this.myTaskWrappers) {
this.myTaskWrappers = this.myTaskWrappers.map(item => {
if(item.Due_Date__c){
// console.log('DATE FORMAT')
let dateobj = new Date(item.Due_Date__c);
if(this.updatedTimeFormat === 'ISO'){
this.isoDate = dateobj.toISOString();
item.Due_Date__c = this.isoDate;
console.log('this.isoDate '+this.isoDate); // 2023-06-29T18:30:00.000Z
let isoDateConvert= new Date(this.isoDate);
console.log('isoDateConvert '+isoDateConvert);
}
else if(this.updatedTimeFormat === 'Absolute'){
this.absoluteDate= dateobj.toString();
console.log('this.absoluteDate '+this.absoluteDate); // Fri Jun 30 2023 00:00:00 GMT+0530 (India Standard Time)
item.Due_Date__c = this.absoluteDate;
}
else{
let dueDateInstance = new Intl.DateTimeFormat(LOCALE,
{ year: 'numeric',
month: 'long',
day: 'numeric'
}).format(dateobj);
item.Due_Date__c = dueDateInstance;
}
}
return item;
});
}
}
- ISO formatting is correct, my doubt is for +3:00 is always showing .000z
- For 2nd time format i.e. 'Absolute', converted time is coming up with DAY and month I want to have in full format. How to achieve the same format which mentioned in above 2nd point.
- For 3rd formatting, logic is not sorted yet in my mind. Any suggestions are welcome.
Please let me know if any other reference of code is needed.