3

When I try to export data to excel in angular, the date in database is in 'x' timezone and I'm exporting data from 'y' timezone. While data is getting exported to excel, date is in 'y' timezone. But i want to keep it same as date in database i.e, date with 'x' timezone only.

I expect output as Mar 26 2014 12:00 AM but I'm receiving this 2014-03-25 23:00:00.000+0000.

private export(records: any[]) {
    // get the export fields
    let exportFields = this.gridConfig.columnDefinitions.map(function (colDef) {
      if(false == colDef.exportField) {
        return;
      }
      return colDef.field;
    });
    let filtered = this.gridOptions.api.getSelectedRows().map(record => {
      return _.pick(this.flatten(record), exportFields);
    });

    const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(filtered);

    let range = XLSX.utils.decode_range(ws['!ref']);
    for(let c = range.s.c; c <= range.e.c; ++c) {
      let address = XLSX.utils.encode_col(c) + "1"; // <-- first row, column number C
      if(!ws[address]) continue;
      ws[address].v = this.findHeaderNameByField(ws[address].v);
    }

    /* generate workbook and add the worksheet */
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'data');

    /* save to file */
    XLSX.writeFile(wb, 'Export.xlsx');
}

When I debugged I found that data from api is only receiving as time with 'y' zone.

Etheraex
  • 508
  • 6
  • 17
Vidya KS
  • 51
  • 5
  • 1
    Add the code where you are doing date formatting or add the relevant code where you are doing export to excel. – sabithpocker Jul 05 '19 at 09:52
  • I have found out one of the solution. In services at backend I changed datatype to LocalDateTime ( which doesn't have timezone), previously it was Timestamp. Now response from api is same as in database. – Vidya KS Jul 10 '19 at 05:24

2 Answers2

0

Use the split based on your input data. This solution will work for all time zones

import * as moment from 'moment';
dateFormat(date): any {
    var string= date;
    var split= str.split(" ", 1);
    return moment(split[0]).format('YYYYMMDD');
}
Etheraex
  • 508
  • 6
  • 17
-1

I know its not the best practice but I just changed the date:

const date = <Date>cell.value;
if (date) {
    const ONE_MINUTE = 60 * 1000;
    const ticks = date.getTime() - date.getTimezoneOffset() * ONE_MINUTE;
    cell.value = new Date(ticks);
}
Shlomi Aharoni
  • 482
  • 7
  • 9