get time back to 12 not 0 - javascript

i have an if statement to change time back to 12 after it hits 11:45:
i = (i >= 192) ? i - 192 : ( i >= 96) ? i - 96 : i
var mins = (i * 15 % 60)
var hours = Math.floor(i * 15 / 60)
var ampm = (hours >= 12) ? "PM" : "AM"
hours = (hours == 0) ? 12 : (hours >= 12) ? hours - 12 : hours;
var nextMins, nextHours = hours;
switch (mins) {
case 0:
mins = "";
nextMins = 15;
break;
case 45:
nextMins = "";
nextHours = hours+1;
break;
default:
nextMins = mins + 15;
break;
}
var time = hours + (mins == "" ? "" : ":" + mins) + " - " + nextHours + (nextMins == "" ? "" : ":" + nextMins) + ampm
it changes in 15 minute intervals, the issue is it will start at 12 but after it gets to 12:00 again it will display as 0:15, 0:30, 0:45. Instead of 12:15, 12:30, 12:45
I thought this part of the if statement would do it:
hours = (hours == 0) ? 12
but isn't working?

The simplest way is probably
hours = (hours % 12) || 12;
This way copes with any positive integer for hours (eg. 36 will still return 12).

It should read
hours = (hours == 0) ? 12 : (hours > 12) ? hours - 12 : hours;
By having >= you're currently including 12 as a number to deduct 12 from.

hours = (hours == 0) ? 12 : hours;
is the complete usage of ternary conditional. But why don't you use a simple if statement?
if(hours == 0)
hours = 12;

Related

How to remove the "day" when the countdown clock is under 24 hours

I'd like the day numbers to disappear when the clock goes below 24 hours instead of having it read "00".
I've attempted a few solutions, but nothing seems to be working for me. I can attach the HTML/CSS if it is needed.
Here is my js:
//
(function(e){
e.fn.countdown = function (t, n){
function i(){
eventDate = Date.parse(r.date) / 1e3;
currentDate = Math.floor(e.now() / 1e3);
//
if(eventDate <= currentDate){
n.call(this);
clearInterval(interval)
}
//
seconds = eventDate - currentDate;
days = Math.floor(seconds / 86400);
seconds -= days * 60 * 60 * 24;
hours = Math.floor(seconds / 3600);
seconds -= hours * 60 * 60;
minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
//
days == 1 ? thisEl.find(".timeRefDays").text("Days") : thisEl.find(".timeRefDays").text("Days");
hours == 1 ? thisEl.find(".timeRefHours").text("Hours") : thisEl.find(".timeRefHours").text("Hours");
minutes == 1 ? thisEl.find(".timeRefMinutes").text("Minutes") : thisEl.find(".timeRefMinutes").text("Minutes");
seconds == 1 ? thisEl.find(".timeRefSeconds").text("Seconds") : thisEl.find(".timeRefSeconds").text("Seconds");
//
if(r["format"] == "on"){
days = String(days).length >= 2 ? days : "0" + days;
hours = String(hours).length >= 2 ? hours : "0" + hours;
minutes = String(minutes).length >= 2 ? minutes : "0" + minutes;
seconds = String(seconds).length >= 2 ? seconds : "0" + seconds
}
//
if(!isNaN(eventDate)){
thisEl.find(".days").text(days);
thisEl.find(".hours").text(hours);
thisEl.find(".minutes").text(minutes);
thisEl.find(".seconds").text(seconds)
}
else{
errorMessage = "Invalid date. Example: 27 March 2015 17:00:00";
alert(errorMessage);
console.log(errorMessage);
clearInterval(interval)
}
}
//
var thisEl = e(this);
var r = {
date: null,
format: null
};
//
t && e.extend(r, t);
i();
interval = setInterval(i, 1e3)
}
})(jQuery);
//
$(document).ready(function(){
function e(){
var e = new Date;
e.setDate(e.getDate() + 60);
dd = e.getDate();
mm = e.getMonth() + 1;
y = e.getFullYear();
futureFormattedDate = mm + "/" + dd + "/" + y;
return futureFormattedDate
}
//
$("#countdown").countdown({
date: "05 December 2019 14:00:00",
format: "on"
});
});
});
I'm not really sure what other details I can provide, but please let me know if there is anything else I can submit to make this easier to answer.

Converting JavaScript ternary operations in dates to conditional statements [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
var timeNow = new Date();
var hours = time.getHours() > 12 ? time.getHours() - 12 : time.getHours();
hours = hours < 10 ? "0" + hours : hours;
function functionHours(){
if(hours > 12) {
return (time.getHours()-12)
} else {
if(hours < 10 && hours > 12) {
return ("0" + hours)
} else {
return (hours)
} else {
return (time.getHours())
}
}
}
I would to convert the ternary operations into if else statements, I've stored the statements on the functions unfortunately it returns an error unexpected token else. What I would like to do is to add 0 to the hours if the hour is 1-9 (E.g. 09, 08, 05, etc...) What seems to be the problem?
Simply make it
var hours = time.getHours();
if (hours < 12)
{
hours = ("0" + hours).slice(-2);
}
No need to check for scenario where hours is greater than 12 at all.
Your logic is off, the following will never be true, because hours can't simultaneously be less than 10 and greater than 12 at the same time:
hours < 10 && hours > 12
Try this version, which is a close conversion of your original ternary expression:
function functionGetHours12(hours) {
if (hours > 12) {
hours = hours - 12
}
if (hours < 10) {
return ("0" + hours)
}
else {
return hours;
}
}
console.log("hour 1: " + functionGetHours12(1));
console.log("hour 9: " + functionGetHours12(9));
console.log("hour 10: " + functionGetHours12(10));
console.log("hour 12: " + functionGetHours12(12));
console.log("hour 15: " + functionGetHours12(15));
console.log("hour 23: " + functionGetHours12(23));
Please check the converted ternary operator to if-else and function:
function functionHours(){
var time = new Date();
var hours = time.getHours() > 12 ? time.getHours() - 12 : time.getHours();
if (time.getHours() > 12) {
hours = time.getHours() - 12;
}
else {
hours = time.getHours();
}
if(hours < 10) {
return "0" + hours;
}
else {
return hours;
}
}
console.log(functionHours());

Auto-updatable values in angularJS

I have array of dates $scope.dates = [] ($scope.dates[0].date). I need to create another array with auto-updateble(!) values of durations.
$scope.dates[i].duration = Date.now() - $scope.dates[i].date.
I want to create timer in seconds:
<tr ng-repeat="x in dates">
<td>{{x.date | date:'HH:mm:ss'}}</td>
<td>{{x.duration}}</td>
Edit: Probled solved
Call this function, it will maintain object in $rootScope object:
$rootScope.timerActivate = function () {
console.log('activateTimer ::');
if(!$rootScope.time)
{
$rootScope.time = {}
}
var countDown = function () {
var today = new Date();
var hours = new Date().getHours();
var hours = (hours + 24) % 24;
var mid = 'am';
if (hours == 0) { //At 00 hours we need to show 12 am
hours = 12;
}
else if (hours > 12)
{
hours = hours % 12;
mid = 'pm';
}
var minute = today.getMinutes();
var sec = today.getSeconds();
$rootScope.time.hours = (hours < 10) ? '0' + hours : hours;
$rootScope.time.minutes = (minute < 10) ? '0' + minute : minute;
$rootScope.time.seconds = (sec < 10) ? '0' + sec : sec;
$rootScope.time.blink = (sec % 2) ? ':' : ' ';
$rootScope.time.mid = mid;
$timeout(countDown, 1000);
};
$timeout(countDown, 1000);
};
You should use $interval. https://docs.angularjs.org/api/ng/service/$interval

Removing Zero from Beginning of 24 Hour Time conversion

This is for a problem on Time Conversion from the 12-hour format to the military/24-hour time.
The standard input: 07:05:45PM
The expected output: 19:05:45
The standard/actual output: 019:05:45
The problem lies in the zero ^ at the beginning of the output.
I tried to set the parseInt() with a radix of 10 for the decimal numeral system but that didn't have any effect.
This is a result of this following code :
function main() {
var time = readLine();
var hours = parseInt(time.substr(0, 2), 10);
var minutes = parseInt(time.substr(3,5));
var seconds = parseInt(time.substr(6,8));
if ( time.indexOf('AM') !== -1 && hours === 12) {
time = time.replace('12', '00');
}
if (time.indexOf('PM') !== -1 && hours < 12) {
time = time.replace(hours, (hours + 12));
}
time = time.replace(/(AM|PM)/g, '');
console.log(time);
}
Any help would be appreciated!
You could just rebuild the string instead of using replace. Because you're using replace in your example you're just replacing the 7 with 19. Using replace will also cause issues if you have a time like 12:12:12.
ex.
function main() {
var time = readLine();
var hours = parseInt(time.substr(0, 2), 10);
var minutes = time.substr(3,5);
var seconds = time.substr(6,8);
if ( time.indexOf('AM') !== -1 && hours === 12) {
hours = 0;
}
else if (time.indexOf('PM') !== -1 && hours < 12) {
hours += 12;
}
time = (hours < 10 ? "0" + hours : hours) + ':' + minutes + ':' + seconds;
console.log(time);
}

jQuery clock 12/24 hour together

I meant to have two clocks, one for 24-hour format, and the other is 12-hour format,
function updateClock() {
var currentTime = new Date();
var currentHoursAP = currentTime.getHours();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHoursAP = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHoursAP = (currentHours == 0) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + " / " + currentHoursAP + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
$("#clock").html(currentTimeString);
}
$(document).ready(function () {
setInterval(updateClock, 1000);
});
but end up to currentHours and currentHoursAP become exact same value.
What do I missed?
The problem was
// Convert an hours component of "0" to "12"
currentHoursAP = (currentHoursAP == 0) ? 12 : currentHoursAP;
if currentHoursAP != 0 then instead of setting back the value of currentHoursAP you were setting it back to currentHours
Demo: Fiddle
getHours is zero-based (0 = 12 AM, 23 = 11 PM), so you need to adjust the check:
currentHoursAP = (currentHours >= 12) ? currentHours - 12 : currentHours;
currentHoursAP = (currentHoursAP == 0) ? 12 : currentHoursAP;

Categories

Resources