2
 <td>{{suite.testSuiteAttributes && 
       suite.testSuiteAttributes.modifiedTimestamp | date: 'yyyy-MM-dd'
     }}
</td>

I want the Date format in "05-Feb-2018 11:00:00 PM CST" CST format.But getting the error:

Unable to convert "2018-01-01-12:12:12:123456" into a date' for pipe 'DatePipe

I think is due to the fact that timeStamp is not in date format..but getting only this date from backend. Please suggest.

TheParam
  • 10,113
  • 4
  • 40
  • 51
sm770
  • 103
  • 1
  • 3
  • 12

2 Answers2

2

I think you are getting the wrong format date from the server. You need a date in the valid format to convert it

So here is a workaround solution to your problem where I have written a myDateParser() method to convert your invalid date to valid date.

your.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  modifiedTimestamp;

 constructor(){

   // Passing unformatter date
   this.modifiedTimestamp = this.myDateParser('2018-01-01-12:12:12:123456');
 }

 /**
  * Custom Date parser

  */
  myDateParser(dateStr : string) : string {
    // 2018-01-01T12:12:12.123456; - converting valid date format like this

    let date = dateStr.substring(0, 10);
    let time = dateStr.substring(11, 19);
    let millisecond = dateStr.substring(20)

    let validDate = date + 'T' + time + '.' + millisecond;
    console.log(validDate)
    return validDate
  }
}

your.component.html

  <table>
    <tr>
     <td>{{modifiedTimestamp |  date: 'yyyy-MM-dd'}}</td>
    </tr>
   </table>

Solution on stackblitz

Hope this will help!

TheParam
  • 10,113
  • 4
  • 40
  • 51
  • What {{suite.testSuiteAttributes && suite.testSuiteAttributes.modifiedTimestamp.toDate() | date: 'yyyy-MM-dd'}} does? It's going to return boolean typed value. – rcanpahali Mar 11 '19 at 09:18
  • buddy is he just checking the testSuiteAttributes is null or not before accessing the modifiedTimestamp – TheParam Mar 11 '19 at 09:21
  • @TheParam Thanks a lot buddy...how can i write it in the CT timeZOne also format like Am/PM? – sm770 Mar 11 '19 at 09:50
  • @TheParam if i change the {{modifiedTimestamp | date: 'dd-MMM-yyyy hh:mm:ss.sss}} the date comes like 2018/01/01 12:12:12.1212...1212 is correct?means can we have 4 digits in miliseconds? – sm770 Mar 11 '19 at 10:03
  • yes date have only 3 milisecond so you can discard the last values – TheParam Mar 11 '19 at 12:51
  • {{modifiedTimestamp | date: 'yyyy-MM-dd h:mm a': 'CST'}} do something like please check updated stackblitz example – TheParam Mar 11 '19 at 12:55
  • What about adding `Z` at the end of the new date string? It works without though. – testing Jan 22 '20 at 13:15
2

Your date "2018-01-01-12:12:12:123456" is not a valid ISO 8601 date, so it cannot be parsed by built in parser. Either use a valid date format or write a custom parser.

You can use a regex or simply use string functions like substring as demonstrated by the other answer.

The dates in Javascript will be in local timezone of browser which is the System time of user, there is no native way to create a date in a different timezone. You can create a date in UTC and use toLocaleString() to convert it to a specific timezone. Depends on whether the date sent from backend is in UTC or CT. If it is CT, then this will work only for users in CT timezone.

let result = "2018-01-01-12:12:12:123456".match(/(\d{4})-(\d{2})-(\d{2})-(\d{2}):(\d{2}):(\d{2}):(\d{3})/).map(x => parseInt(x, 10));

result.shift();

console.log(new Date(...result).toLocaleString())
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • ..this is the date i am getting from backend...so can't do anything about it...could u please brief me a bit on writing the custom parser. – sm770 Mar 11 '19 at 09:33
  • Yes, but can you explain what 123456 represent here? the millisecond part can have max 3 chars. `hh:mm:ss.sss`. There is no concept of millisecond greater than 999. – sabithpocker Mar 11 '19 at 09:35
  • it is dummy data might be incorrect also ,,,can u please help with the pareser content considering it only three digits to get it like 05-Feb-2018 11:00:00 PM CST – sm770 Mar 11 '19 at 09:40