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

20,621

Solution 1

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!

Solution 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())
Share:
20,621
sm770
Author by

sm770

I am a UI developer with Angular Stack , using html/css/javascript/typeScript/Angular2-7

Updated on March 12, 2020

Comments

  • sm770
    sm770 about 4 years
     <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.

    • Hakier
      Hakier about 5 years
      You should convert a value to Date where you obtain them from backend or write a Pipe that will be converting this for you.
    • rcanpahali
      rcanpahali about 5 years
      Are you trying to concatenate texts?
    • sm770
      sm770 about 5 years
      @Hakier ok i will try for the pipe approach
    • sm770
      sm770 about 5 years
      @shadowman_93 no..using && for handling null values in the array
    • TheParam
      TheParam about 5 years
      modifiedTimestamp will be your date right?
    • sm770
      sm770 about 5 years
      @TheParam yes it is the date..like this..2018-01-01-12:12:12:123456”
    • TheParam
      TheParam about 5 years
      date which you trying to parse is invalid date
    • sm770
      sm770 about 5 years
      @TheParam how can it be converted to a valid date??
    • TheParam
      TheParam about 5 years
      write you need to write the custom parser or ask backend to team to provide valid timestamp
    • TheParam
      TheParam about 5 years
      I have written a simple parser for you please check answer
    • Ian Kemp
      Ian Kemp about 5 years
      Then fix your backend to provide a valid date.
    • sm770
      sm770 about 5 years
      @IanKemp ..thanks..once i get the valid date , how can i convert it into CT time zone??
  • rcanpahali
    rcanpahali about 5 years
    What {{suite.testSuiteAttributes && suite.testSuiteAttributes.modifiedTimestamp.toDate() | date: 'yyyy-MM-dd'}} does? It's going to return boolean typed value.
  • TheParam
    TheParam about 5 years
    buddy is he just checking the testSuiteAttributes is null or not before accessing the modifiedTimestamp
  • sm770
    sm770 about 5 years
    ..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.
  • sabithpocker
    sabithpocker about 5 years
    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.
  • sm770
    sm770 about 5 years
    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
    sm770 about 5 years
    @TheParam Thanks a lot buddy...how can i write it in the CT timeZOne also format like Am/PM?
  • sm770
    sm770 about 5 years
    @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?
  • TheParam
    TheParam about 5 years
    yes date have only 3 milisecond so you can discard the last values
  • TheParam
    TheParam about 5 years
    {{modifiedTimestamp | date: 'yyyy-MM-dd h:mm a': 'CST'}} do something like please check updated stackblitz example
  • testing
    testing over 4 years
    What about adding Z at the end of the new date string? It works without though.