0

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.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Arushi
  • 137
  • 6

4 Answers4

1
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;
}
Aditya_kr
  • 175
  • 1
  • 13
1

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

Durga
  • 15,263
  • 2
  • 28
  • 52
0
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');
BeginnerBro
  • 195
  • 2
  • 18
  • its showing time but after adding this var starttimess = ('00' + starttimes.getUTCHours()).slice(-2) + ':' + ('00' + starttimes.getUTCMinutes()).slice(-2); its not showing alert and time – Arushi Jul 11 '17 at 07:08
  • @Arushi why are you declaring same variable name twice and if you need to convert your date to hh:mm then my answer above is enough – BeginnerBro Jul 11 '17 at 07:31
0

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

That means you can do this:

var time = moment(date).format("hh:mm a");
Titus
  • 22,031
  • 1
  • 23
  • 33
  • its showing time but after adding this var starttimess = ('00' + starttimes.getUTCHours()).slice(-2) + ':' + ('00' + starttimes.getUTCMinutes()).slice(-2); its not showing alert and time – Arushi Jul 11 '17 at 07:08