Skip to content

Instantly share code, notes, and snippets.

@sunnyg1210
Last active June 26, 2019 14:42
Show Gist options
  • Save sunnyg1210/ff89718e26a3c6d2b746d7068f4ec53c to your computer and use it in GitHub Desktop.
Save sunnyg1210/ff89718e26a3c6d2b746d7068f4ec53c to your computer and use it in GitHub Desktop.
Convert unix date to human readable date in Angular 2+
import { Pipe, PipeTransform } from '@angular/core';
/*
* Author: Sunny Gohil
* Takes a Unix Date and converts it to string format
* Usage:
* unixDate
* Example:
* {{ 1561560050 | unixDate}}
* formats to: 2019-06-26
*/
@Pipe({
name: 'unixDate'
})
export class UnixDatePipe implements PipeTransform {
transform(unixTime: any, args?: any): any {
const utcSeconds = unixTime;
var date = new Date(0);
date.setUTCSeconds(utcSeconds);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const newDate = year + '-' + month + '-' + day;
return newDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment