Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covert to 24 hours time formate #2959

Closed
iamkallolpratim opened this issue Feb 11, 2016 · 4 comments
Closed

Covert to 24 hours time formate #2959

iamkallolpratim opened this issue Feb 11, 2016 · 4 comments

Comments

@iamkallolpratim
Copy link

var start_time = $('.start_time').val();

this will return time like 02:00 PM

How can I convert it 24 hours time format using moment js. I used this plugin to show time.

@mattjohnsonpint
Copy link
Contributor

moment("02:00 PM", "h:mm A").format("HH:mm")  // "14:00"

@mattjohnsonpint
Copy link
Contributor

Also note that you might not need moment.js at all, as there is an option for 24-hour format in that plugin.

See the showMeridian option in their docs

@iamkallolpratim
Copy link
Author

thank you very much @mj1856 . It's working

@farrukh-malik
Copy link

How to use moment.js durations rightly?
|
Use moment.duration() in codes

You need to import moment and moment-duration-format, firstly.

import moment from 'moment';
import 'moment-duration-format';

Then, use duration function. Let us apply the above example: 28800 = 8 am.

moment.duration(28800, "seconds").format("h:mm a");

🎉Well, you do not have above type error. 🤔Do you get a right value 8:00 am ? No…, the value you get is 8:00 a. Moment.js format is not working as it is supposed.

💡The solution is that transform seconds to milliseconds and use UTC time.

moment.utc(moment.duration(value, 'seconds').asMilliseconds()).format('h:mm a')

All right we get 8:00 am now. If you want 8 am instead of 8:00 am for integral time, we need to do RegExp

const time = moment.utc(moment.duration(value, 'seconds').asMilliseconds()).format('h:mm a');
time.replace(/:00/g, '')

IF YOU NEED MORE HELP COMMENT HERE..!

THANK YOU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants