Come convertire una stringa in data in Angular2 \ Typescript?

Voglio creare un nuovo oggetto Date, con una data specifica. Ho pensato di convertirlo da una stringa specifica, per esempio:

let dateString = '1968-11-16T00:00:00'

Come posso convertirlo in un oggetto data in typescript?

Aggiornamento:

Sto chiedendo una soluzione in Typescript e non in Javascript, e in Angular2, non AngularJS (1.x)

Soluzione

È possibile utilizzare il filtro data per convertire in data e visualizzare in formato specifico.

Nel file .ts (typescript):

let dateString = '1968-11-16T00:00:00' 
let newDate = new Date(dateString);

In HTML:

{{dateString |  date:'MM/dd/yyyy'}}

Qui sotto ci sono alcuni formati che puoi implementare:

Backend:

public todayDate = new Date(Date.parse(Date()));

HTML :


<select>

[{{todayDate | date:'MM/dd/yyyy'}}]
[{{todayDate | date:'EEEE, MMMM d, yyyy'}}]
[{{todayDate | date:'EEEE, MMMM d, yyyy h:mm a'}}]
[{{todayDate | date:'EEEE, MMMM d, yyyy h:mm:ss a'}}]
[{{todayDate | date:'MM/dd/yyyy h:mm a'}}]
[{{todayDate | date:'MM/dd/yyyy h:mm:ss a'}}]
[{{todayDate | date:'MMMM d'}}]   
[{{todayDate | date:'yyyy-MM-ddTHH:mm:ss'}}]
[{{todayDate | date:'h:mm a'}}]
[{{todayDate | date:'h:mm:ss a'}}]      
[{{todayDate | date:'EEEE, MMMM d, yyyy hh:mm:ss a'}}]
[{{todayDate | date:'MMMM yyyy'}}] 
Commentari (2)