converting long timestamp to extract year in typescript
typescript timestamp type
convert timestamp to date angular 6 typescript
From my backend I get an object with date of birth that is a long value.For frontend I use angular 4 (typescript) I would like to extract year from that date of birth to be able to calculate age but I have no idea how to parse long to some kind of Date object in typescript. Could you give me a hint where to look for information?
Maybe something along lines of:
a: Number; let a = new Date(762861060).getFullYear();
Thank you for help
You're not far off, if you don't need fractional years:
var birthDate = new Date(762861060); var laterDate = new Date(); var yearsBetween = laterDate.getFullYear() - birthDate.getFullYear();
You can try simple code like this:
var birthDate = new Date(762861060); var todayDate = new Date(); var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds; var ageInDays = (todayDate - birthDate) / milliDay; var ageInYears = Math.floor(ageInDays / 365 ); console.log(ageInYears)
See more related answers on this and this questions...
You can use Moment.js:
import * as moment from 'moment'; var diff = moment.duration((moment(762861060)).diff(moment())); var age_in_days = diff.days(); var age_in_years = diff.years();
Typescript Date Object, Typescript Date Object with typescript tutorial, typescript introduction, versions, It allows us to get or set the year, month and day, hour, minute, second, and millisecond. toLocaleFormat(), It converts a date to a string, using a format string. An integer number representing the number of milliseconds that has passed since the beginning of 1970 is called a timestamp. It’s a lightweight numeric representation of a date. We can always create a date from a timestamp using new Date(timestamp) and convert the existing Date object to a timestamp using the date.getTime() method (see below).
DateStr – A Strongly-Typed Date String for TypeScript, The API would use those dates in an SQL query to retrieve the correct results. For example, PostgreSQL has a date type, a timestamp type (date and time), and a which takes a year, month, and day, expects the month to be 0-based. to tell TypeScript that a valid string is in fact a DateStr as long as it Checking that the string matches the regular expression is a good start, but it still allows invalid dates to be passed, like “2017-13-99”. To prevent this, extract the year, month and day, and check if month is between 1 and 12, and if day is between 1 and the number of days in the month of that year.
- Welcome to Stack Overflow, please try adding some code, then we can start trying to help you! :)
- I edited my post