converting long timestamp to extract year in typescript

javascript convert timestamp to date timezone
javascript convert timestamp to date format
javascript timestamp format
moment#format
javascript date
javascript date format
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();

Demystifying DateTime Manipulation in JavaScript, You can then format it to extract only the date part as follows: var currentDate getFullYear(); var dateString = date + "-" +(month + 1) + "-" + year; In JavaScript, a timestamp is the number of milliseconds that have passed since January 1, 1970. Converting a string to a JavaScript date object is done in different ways. We have a moment library to handle and manipulate Date objects using typescript and javascript. We will see the example to convert Date to string or String to Date in typescript. How to Convert String to Date in typescript? We have many ways to convert to String to date. Using Date constructor

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...

Docs, As of version 2.13.0, Moment includes a typescript definition file. YYYY from version 2.10.5 supports 2 digit years, and converts them to a year near 2000 To create a moment from a Unix timestamp (seconds since the Unix Epoch), use  Convert your timestamp to datetime or date to timestamp with single click Unlike other languages, JavaScript doesn't have equivalent for function strftime . Javascript however provides multiple functions to present date and time in human readable form.

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();

How to convert Unix timestamp to time in JavaScript?, The toUTCString() method is used to represent the Date object as a string the UTC time format. The time from this date string can be found by extracting from the  Supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds. #N#Human date to Timestamp. [batch convert] Input format: R FC 2822, D-M-Y, M/D/Y, Y-M-D, etc. Strip 'GMT' to convert to local time. Prefer a 12-hour clock? Go to preferences. Press c to clear all forms. Epoch dates for the start and end of the year/month/day.

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).

JavaScript Date, year: Numeric value to represent year of a date. month: Numeric value to represent month The following example converts date string to DD-MM-YYYY format. Dear sir: I really apreciate the post wich is very good and informative. However, I have a question about date time calculations: I have a datatabel, in wich there are the start datetime and end date time and I need to compare them to see if the present time is between the above two (if the present time is less than the end time and more than the start time), so I can fire an alarm.

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.

Comments
  • Welcome to Stack Overflow, please try adding some code, then we can start trying to help you! :)
  • I edited my post