'Can't format moment object to date and time
I'm using a react component for Date picking which returns a moment object. Also, I'm using my custom component to select time and saving the value like this for example 3:30 pm .
What I want to do is make this one moment object that I can later user and format however I want.
This is my current implementation but it's not working:
formatting the time is always returning AM
const time = moment(pickupTime.formattedTime, ['hh:mm a'])
.locale('en')
.format('hh:mm A');
const dateTime =
startDay.locale('en').format('MMM Do YY') + // This is the Date which is a moment object
' ' +
time;
Solution 1:[1]
Because string + string = string, you cannot get an object by concatenating strings.
What you need is to convert a string to a moment object:
moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss");
Result:
Moment {_isAMomentObject: true, _i: '24/12/2019 09:15:00', _f: 'DD MM YYYY hh:mm:ss', _isUTC: false, _pf: {…}, …}
Check out https://momentjs.com/docs/#/parsing/string-format/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kien_coi_1997 |
