0

I am new to ionic, typescript and angularjs. In regular java parsing long date is easy

    Date date = new Date(1915654554);
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String strDate = dateFormat.format(date); 

But in Ionic-TypeScript, i tried like this

    let date=new Date(1915654554);  
    alert(date);   //shows invalid date

Actually i am getting "PublishingDate": "/Date(1497426961890)/" from web-api

HTML code :

       <p>
         {{ getActualDate(notice.PublishingDate) }}
      </p>  

TypeScript code:

       getActualDate(lDate)
      {
           var d=lDate.substring(6,19);
           let date=new Date(d)
          return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date. getFullYear();

                  //shows Nan/Nan/Nan
      }

its showing invalid date. any help, suggestions please. Thank you.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Onkar
  • 679
  • 1
  • 8
  • 25

1 Answers1

1

Try the following. You can use the Javascript Date Methods.

let date=new Date(1915654554);  
alert(date.getDate() + "/" + (date.getMonth() + 1) + "/" + date. getFullYear()); // 22/1/1970

The more important thing to remember is getMonth returns a number between 0-11, so you have to add 1 to it if you want it to be 1-12.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • 1
    @faceturn I disagree with that duplicate question. Although similar the date format is different. The accepted answer on the duplicate question is a much longer format than OP asked about which was dd/MM/yyyy format. Also my answer was posted before the duplicate question was marked. I was not aware of that question already being asked. Although the two questions are different because of that format. – Charlie Fish Dec 23 '17 at 08:28
  • @CharlieFish i edited my question, its showing Nan/Nan/Nan like – Onkar Dec 23 '17 at 08:37
  • 1
    @Onkar You need a parseInt, for example `let date=new Date(parseInt(d))`, you also have a typo when declaring `d`. You need to create the date from a number not a string. – Charlie Fish Dec 23 '17 at 08:44