简体   繁体   中英

How to convert string to time format in React js

I fetch data from SQLite db like "16:00" as string now I want to convert to time format and add 10 minutes. How can I do this in react js?

This is not ReactJS task:)

You can work with dates using JavaScript, but the API is not "very good" (IMHO).

I can suggest Moment.js as I usually use it, but there are many different libs to work with Date/Time easily.

You can add Moment to your app:

npm install moment --save   # npm
yarn add moment             # Yarn

Moment API example:

const date = moment.utc()
                   .hour(16)   // numbers from 0 to 23
                   .minute(0); // numbers from 0 to 59
const plus10Min = date.add(10, 'm')

Here you can find very nice documentation with a lot of examples.

Assuming that you know the year, month and day - this is what you can do

var hour = row[<index of the column>].split(":")[0];
var minute = row[<index of the column>].split(":")[1];

var updatedMinute = parseInt(minute) + 10;

var date = new Date(year,month,day, hour, updatedMinute+'');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM