typescript 如何在Angular中将时间戳转换为日期格式?

xfyts7mz  于 2022-11-30  发布在  TypeScript
关注(0)|答案(2)|浏览(284)

我想把时间戳转换成日期格式,这是“日/月/年”,但我结束了在图片中的日期格式。我也使用同步电子表格。

export-electronic.component.ts

updatedata(){
        this.dataApi.getElectronicById(this.id).subscribe(res => {
          this.electronicObj = res;
          console.log(this.electronicObj);
          this.spreadsheetObj.updateCell({ value: 
          this.electronicObj.mantainence_history},"O3");
          this.spreadsheetObj.updateCell({ value: 
          this.electronicObj.cost_status},"P3");
          this.spreadsheetObj.updateCell({ value: this.electronicObj.warranty_date.toDate()+this.electronicObj.expire_date.toDate()},"Q3");}

export-electronic.component.html

<ejs-spreadsheet #spreadsheet  (created)="created()" (openComplete)="updatedata()" openUrl='https://ej2services.syncfusion.com/production/web-services/api/spreadsheet/open' allowOpen='true' (beforeSave)='beforeSave($event)' saveUrl='https://ej2services.syncfusion.com/production/web-services/api/spreadsheet/save'  allowSave='true'> 
</ejs-spreadsheet>

在excel单元格中。我希望答案像这样。示例29/11/2022 - 01/12/2022谢谢你的建议

答案

updatedata(){
    this.dataApi.getElectronicById(this.id).subscribe(res => {
        this.electronicObj = res;
        console.log(this.electronicObj);
        const q3Value = (new Date(this.electronicObj.warranty_date.toDate()).toLocaleDateString('en-GB')) + ' - ' + (new Date(this.electronicObj.expire_date.toDate()).toLocaleDateString('en-GB'));
        this.spreadsheetObj.updateCell({ value: 
        this.electronicObj.mantainence_history},"O3");
        this.spreadsheetObj.updateCell({ value: 
        this.electronicObj.cost_status},"P3");
        this.spreadsheetObj.updateCell({ value: q3Value},"Q3");
    });
}

jyztefdp

jyztefdp1#

您可以将日期字符串(或时间戳)转换为JS日期对象,并根据自己的喜好设置其格式。下面是一个将日期设置为“dd/mm/yyyy”格式的示例:

new Date('Mon Nov 9 13:29:40 2012').toLocaleDateString('en-GB');

这里的语言环境'en-GB'确保您总是得到dd/mm/yyyy。
因此,您可以像这样更新代码:

updatedata(){
    this.dataApi.getElectronicById(this.id).subscribe(res => {
        this.electronicObj = res;
        console.log(this.electronicObj);
        const q3Value = (new Date(this.electronicObj.warranty_date).toLocaleDateString('en-GB')) + ' - ' + (new Date(this.electronicObj.expire_date).toLocaleDateString('en-GB'));
        this.spreadsheetObj.updateCell({ value: 
        this.electronicObj.mantainence_history},"O3");
        this.spreadsheetObj.updateCell({ value: 
        this.electronicObj.cost_status},"P3");
        this.spreadsheetObj.updateCell({ value: q3Value},"Q3");
    });
}

在这里,您将在推入电子表格之前创建格式化的日期范围。查找变量q3 Value

8oomwypt

8oomwypt2#

你可以试试
1.管道

getFormatedDate(date: Date, format: string) {
    const datePipe = new DatePipe('en-US'); // culture of your date
    return datePipe.transform(date, format);
}
  1. moment.js
let parsedDate = moment(dateStr,"MM-DD-YYYY");
    let outputDate = parsedDate.format("DD-MM-YYYY");

1.简单拆分

const str = '10-23-2022';

const [month, day, year] = str.split('-');

console.log(month); // 👉️ "10"
console.log(day); // 👉️ "23"
console.log(year); // 👉️ "2022"

const date = new Date(+year, +month - 1, +day);

console.log(date);

相关问题