scripter
scripter

Reputation: 1526

How to change time format using momentjs?

So I want to change time 06:00:00 to 06:00 pm .I heard momentjs is a great library for date and time formatter. But I am not able to figure out how to use it. can anyone give me a hint on that

Upvotes: 0

Views: 1156

Answers (2)

Aethyn
Aethyn

Reputation: 705

For Node JS :
Use NPM :

npm install moment

Then in your app.js :

var moment = require('moment');
moment().format();

Or if you're browser-side only, add in your page :

<script src="moment.js"></script>
<script>
    moment().format();
</script>

Then use the format() method to apply your format as described in :
format() | moment.js docs

EDIT :
for your format (ex : 6:00 PM) the code is :

moment().format("h:mm a");
// OR (depending of the needed caps lock for "AM/PM"
moment().format("h:mm A");

EDIT 2 :
To format your time, do something like :

var currentDate = moment.now();
var formatDate = moment(currentDate).format('h:mm A');

Upvotes: 2

Rafi Ud Daula Refat
Rafi Ud Daula Refat

Reputation: 2257

You can do this. I have showed the output in a div.

var momentObj = moment("13:15", ["HH:mm"])
document.getElementById("output").innerText = momentObj.format("h:mm A");

I have created a fiddle. have a look.

https://jsfiddle.net/Refatrafi/f1y15mqg/

Upvotes: 2

Related Questions