0

I am using primeNG calendar and I got a model called myDate and a dateformat. ngModel directive referencing to a Date property.

<p-calendar [(ngModel)]="myDate"  dateFormat="dd/mm/yy"></p-calendar>

But problem is I want to store myDate value as Unix Timestamp. So I need to convert myDate to milliseconds before set and convert it to date object with dateformat before get operation. Is there any way to do this?

private myDate;

setMyDate(myNewDate){
    this.myDate = convertDateToTimestamp(myNewDate)
}

getMyDate(){
   return convertTimestampToDate(this.myDate)
}
hellzone
  • 5,393
  • 25
  • 82
  • 148

2 Answers2

0

You can call getTime() on this date object to get it in unix form. It comes out in milliseconds.

new Date("2013/09/05 15:34:00").getTime();

It may have decimal bits so wrapping it in Math.round would clean that.

Math.round(new Date("2013/09/05 15:34:00").getTime());

how to convert a string to a Unix timestamp in javascript?

0

You could use getters and setters to achieve this, which is actually very close to what you already have:

private _myDate;

set myDate(myNewDate){
    this._myDate = convertDateToTimestamp(myNewDate)
}

get myDate(){
   return convertTimestampToDate(this._myDate)
}
JonMac1374
  • 466
  • 2
  • 7