I'm using moment.js and want to update a date-time string with a new user-entered time. The date has not changed, only the time. There is no timezone change, just that the hour and minute values have possibly been altered.
How would I take a string like this and convert it such that the time is different?
This is would I'd expect:
const dateTimeString = '2017-11-14T16:04:54.0086709-06:00'
const newDateTimeString = (
moment(dateTimeString)
.changeTime('05:20 PM')
.format()
)
// newDateTimeString === '2017-11-14T17:20:00.0086709-06:00'
If the user-entered time is formatted as such
13:00
you could do :Using https://momentjs.com/docs/#/get-set/hour/ and https://momentjs.com/docs/#/get-set/minute/
If it's formatted with PM and AM system, you could to the same, but with a little bit more of parsing, to know if it's 5.00 AM or PM.
There is no built in function like
changeTime
, you can write your own usingset
.You can add
changeTime
usingmoment.fn
You can create a temp moment object with your "time to add" value using
moment(String, String)
, then useset(Object(String, Int))
and getters likehours()
andminutes()
.Here a live sample:
One possible way is to use moment to grab the date and then combine it with your custom time value in another
moment()
call like so: