This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 5 years ago.
I'd like my hours section to be set to 1–12 and not 0–23. Thank you.
Here's the JavaScript:
setInterval(function(){ time();}, 1000)
function time(){
var dates = new Date();
var newDates = dates.toDateString();
// var clock = dates.toLocaleTimeString();
var seconds = dates.getSeconds();
var minutes = dates.getMinutes();
var hours = dates.getHours();
var stringSeconds= String(seconds);
var stringMinutes= String(minutes);
var stringHours= String(hours);
newDate.textContent = newDates;
newDivSeconds.textContent = stringSeconds;
newDivMinutes.textContent = stringMinutes + ' :' ;
newDivHours.textContent = stringHours + ' :';
const usHours = (date.getHours() % 12) || 12;
Use the modulus operator
var usHours = date.getHours() % 12;
Related
This question already has answers here:
How to get 2 digit year w/ Javascript? [duplicate]
(5 answers)
Closed 2 years ago.
Caould anbody please help me, how I can change my javascript code to show year 20 not 2020. I am trying to add this code
d = Date.now();
d = new Date(d);
d = d.getDate()+'.'+(d.getMonth()+1)+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()
console.log(d);
But I cannot it get it write, how i should add this code to my code to get it right,. Thank you in advice
<script>
const pad = num => ("0" + num).slice(-2);
const timedate = () => {
const currentTime = new Date(new Date().getTime() + diff);
let hours = currentTime.getHours();
const minutes = pad(currentTime.getMinutes());
const seconds = pad(currentTime.getSeconds());
const d = currentTime.getDate();
console.log(d);
const day = pad(d);
const month = pad(currentTime.getMonth() + 1);
const yyyy = currentTime.getFullYear();
/* let dn = "PM"
if (hours <= 12) dn = "AM";
if (hours >= 12) hours -= 12;
if (hours == 0) hours = 12; */
hours = pad(hours);
timeOutput.value = "" +
yyyy + "." + month + "." + day +
" " +
hours + ":" +
minutes + ":" +
seconds// + dn;
}
let timeOutput;
let serverTime;
let diff;
window.addEventListener("load", function() {
timeOutput = document.getElementById("timedate");
serverTime = new Date;// change to new Date("[[:Date:]]"); for example
diff = new Date().getTime() - serverTime.getTime();
setInterval(timedate, 1000);
});
</script>
new Intl.DateTimeFormat("en", {year: "2-digit"}).format(new Date())
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am using the below code to get the current time in angularJS:
$scope.getDatetime = function() {
return (new Date()) + "abc.txt" ;
};
What is the correct code to the current time in YYYY-MM-DD-Hours-Minutes-Seconds?
Maybe you need this.
$scope.getDatetime = function() {
var date = new Date();
var day = date.getDate();
var month = ((date.getMonth() + 1) > 9) ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds;
};
var s = new Date().toISOString();
console.log(s);
s = s.slice(0,19);
s = s.replace('T','-');
s = s.replace(':','-');
s = s.replace(':','-');
console.log(s);
This question already has answers here:
Compare two dates with JavaScript
(44 answers)
How can I compare two time strings in the format HH:MM:SS?
(16 answers)
Closed 4 years ago.
How to find the difference between to times in JavaScript?
Here is the pseudocode I've come up with
var firstTime = "20:24:00";
var secondTime = "20:00:52";
console.log(firstTime - secondTime);// 23:08 (23 minutes, 8 seconds)
You could use new Date().setHours() to make to dates from the time you have and then subtract them, make a new date from the difference:
var firstTime = "20:24:00";
var secondTime = "20:00:52";
// transform variables into parameters
let dateA = new Date().setHours(...(firstTime.split(":")));
let dateB = new Date().setHours(...(secondTime.split(":")));
let diff = new Date(dateA-dateB);
console.log(`Differnce: ${diff.getUTCHours()}:${diff.getUTCMinutes()}:${diff.getUTCSeconds()}`);
Try this:
var myDate1 = new Date();
myDate1.setHours(20, 24, 00, 0);
var myDate2 = new Date();
myDate2.setHours(20, 00, 52, 0);
If you subtract them directly, it will give you a timestamp value. You can convert this value by saying:
var result = myDate1 - myDate2; // returns timestamp
var hours = new Date(result).getHours(); // returns hours
A while ago I had made a function similar to the one described:
let timeOp = function(operation, initial, value) {
// define the type of operation in bool if needed
if(typeof operation == "string") {
var operation = (operation == 'add') ? true : false;
}
// convert to minutes `value` if needded
if(!Number.isInteger(value)) {
var time = value.split(':');
value = parseInt(time[0]) * 60 + parseInt(time[1]);
}
// split the string and get the time in minutes
var time = initial.split(':');
time = parseInt(time[0]) * 60 + parseInt(time[1]);
// add or substract `value` to minute
time += (operation) ? value : -value;
// standardise minutes into hours
var hour = Math.floor(time / 60);
var minute = time % 60;
// return with '0' before if needed
return hour + ':' + ((minute>=10) ? minute : ('0' + minute))
}
let firstTime = "20:24";
let secondTime = "20:00";
console.log(timeOp('substract', firstTime, secondTime)
It's not perfect and it doesn't allow to use seconds. But you can figure that out pretty easily by modifying the above code.
This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 5 years ago.
I have an example below of code that getting the date and adding certain days.
But the result I got from log is like these 1507824000000.
var endDate = new Date('10/03/2017');
var numOfDays = 10;
console.log(endDate.setDate(endDate.getDate() + numOfDays ));
If you want something to see your 10 days added, you can try the following :
var endDate = new Date('10/03/2017');
var numOfDays = 10;
endDate.setDate(endDate.getDate() + numOfDays);
var dd = endDate.getDate();
var mm = endDate.getMonth() + 1;
var y = endDate.getFullYear();
var yourNewDate = dd + '/'+ mm + '/'+ y;
console.log(yourNewDate)
This question already has answers here:
convert 12-hour hh:mm AM/PM to 24-hour hh:mm
(38 answers)
Closed 8 years ago.
I have the time which is in the format "07:30PM" .I need to convert into 24 hr format using javascript to do further calculations.Please assist me in doing this.
Duplicate of
this
Simple solution:
function ampmTo24(time)
{
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AP = time.match(/\s(.*)$/);
if (!AP) AP = time.slice(-2);
else AP=AP[1];
if(AP == "PM" && hours<12) hours = hours+12;
if(AP == "AM" && hours==12) hours = hours-12;
var Hours24 = hours.toString();
var Minutes24 = minutes.toString();
if(hours<10) Hours24 = "0" + Hours24;
if(minutes<10) Minutes24 = "0" + Minutes24;
return Hours24 + ":" + Minutes24
}