Arushi
Arushi

Reputation: 137

convert time through javascript and jquery in hrs

With this Code, I am getting the time as: 14:00

var starttime = ('00' + starttime.getUTCHours()).slice(-2) + ':' + ('00' + starttime.getUTCMinutes()).slice(-2);
var endtime = ('00' + endtime.getUTCHours()).slice(-2) + ':' + ('00' + endtime.getUTCMinutes()).slice(-2);

But I want Time in form of 12 hrs Like: 2:00 PM. What are the possibilities in javascript and jQuery? Help me I am newbie in this field.

Upvotes: 0

Views: 360

Answers (4)

BeginnerBro
BeginnerBro

Reputation: 195

var ts = new Date(your timestamp);

    ts = moment(ts).format('hh:mm a');

where H means 24 hrs and you want h using moment.js library

in your case,

starttime = moment(starttime ).format('hh:mm a');
endtime = moment(endtime).format('hh:mm a');

Upvotes: 0

Durga
Durga

Reputation: 15614

var date = new moment();
var time = date.format('hh:mm a');
console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>

use .format('hh:mm a') to get in 12 hour format

Upvotes: 1

Titus
Titus

Reputation: 22474

You've said you're using moment.js.

That means you can do this:

var time = moment(date).format("hh:mm a");

Upvotes: 0

Aditya_kr
Aditya_kr

Reputation: 175

function fancyTimeFormat(time)
{   
    // Hours, minutes and seconds
    var hrs = ~~(time / 3600);
    var mins = ~~((time % 3600) / 60);
    var secs = time % 60;

    // Output like "1:01" or "4:03:59" or "123:03:59"
    var ret = "";

    if (hrs > 0) {
        ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
    }

    ret += "" + mins + ":" + (secs < 10 ? "0" : "");
    ret += "" + secs;
    return ret;
}

Upvotes: 1

Related Questions