Last active
June 26, 2019 14:42
-
-
Save sunnyg1210/ff89718e26a3c6d2b746d7068f4ec53c to your computer and use it in GitHub Desktop.
Convert unix date to human readable date in Angular 2+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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