S.Troy
S.Troy

Reputation: 117

How do I change a string value to a timestamp? Javascript React-Native

I have a start date job that I want to add an amount of days to, that the user enters. ( as a duration for the job) I want to find out what the last date of the job will be by adding the duration to the starting date.

The starting date is being saved as a timestamp but the duration as a string.

Can I add the two together and get a final value as a timestamp?

Upvotes: 0

Views: 817

Answers (1)

Hamas Hassan
Hamas Hassan

Reputation: 941

First convert the timestamp into date object then add the duration in the date after that again convert it back to time stamp

Example

const timestamp = 1629640800000

const date = new Date(timestamp * 1000);
console.log(date) // Wed Apr 06 53611 13:00:00

const hours = date.getHours() 
console.log(hours) // 13

//suppose you want to add 4 hours in the existing date-time

date.setHours(hours + 4);
console.log(date) // Wed Apr 06 53611 17:00:00

// now converting back to timestamp

Date.parse(date)
console.log(date) // 1629640814400000

Upvotes: 1

Related Questions