I want to create a pipe for angular2, and this is the code:
@Pipe({name: 'stringToDate'})
export class StringToDatePipe implements PipeTransform {
/**
* Constructor
*/
constructor() {
}
/**
* Transform a date that is passed as string into a date
* @param value The date passed as string
* @returns {Date} The Date object
*/
transform(value: string): Date {
console.log(value);
let d = new Date(value);
console.log(d);
return d;
}
}
I don't know why it is not creating the correct date. This is what console prints:
2016-01-01
Thu Dec 31 2015 21:00:00 GMT-0300
How can I fix it?
yyyy-mm-ddstring, and thenew Date(value)converts the date string to aDateobject. Then when theDateobject is logged to the console, it is formatted in the current time zone. Since the original string did not specify a time zone, theDate()function assumed GMT. So the Date object contains a 2016-01-01 GMT time, and displays it as a GMT-03 time -- is that the time zone of your PC?