Abdul Rehman
Abdul Rehman

Reputation: 5654

convert python timestamp into minutes using JavaScript react

I'm working on a project using python(3.7) and React in which I'm getting a timestamp from python and need to display it inside react component and need to display it as minutes.

Here's what I tried:

{Math.round((new Date().getTime() - new Date(message.timestamp).getTime())/60000)} minutes ago

it displayed as:

NaN minutes ago

If I display it as:

{message.timestamp}

then it returns:

2019-04-09 13:01:22.036902+00:00

So, how can I display only minutes from that timestamp?

Upvotes: 1

Views: 702

Answers (2)

Revansiddh
Revansiddh

Reputation: 3062

Make use of momentjs library and format your string as you want

const a = "2019-04-09 13:01:22.036902+00:00"
console.log(moment(a).format('hh:mm A'))
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

Using Date

var a = new Date("2019-04-09 13:01:22.036902+00:00")


a.getMinutes()

console.log("hours:"+a.getHours(),"minutes:"+a.getMinutes())

As per comment If your looking for difference between two timestamps then

const timediff= moment().diff(moment("2019-04-09 13:01:22.036902+00:00"),'minutes')


console.log('timediff',timediff)
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>

Upvotes: 1

dorriz
dorriz

Reputation: 2679

You could try

{Math.round((new Date().getTime() - Date.parse(String(message.timestamp)))/60000)} minutes ago

I think that should work

Upvotes: 0

Related Questions