formet in 24 hour inn clock function - javascript

Hello every one I am writing a abir John and want to find the clock Javascript file
That's code add in javascript file then link in index.html file
function clock(){
var hour = document.getElementById('hour');
var minute = document.getElementById('minute');
var seconds= document.getElementById('seconds');
var amp= document.getElementById('amp');
if(h>12){
h =h-12;
var am = "PM"
} ```

I think you are trying to convert 24 hrs format to 12 hrs format.
If you want to convert your hour to 12 hours format you can take % 12 on the current time.
If the time is 13 then 13 % 12 → 1
time = 23 then 23 % 12 → 11
time = 24, then 24 % 12 → 0, if the time is 0, then change the time as 12.
if(h>=12){
h = h%12 || 12;
}

Related

How can I execute a condition using new Date in Javascript?

I have a curTime variable that is the current time using new Date() and a pwChangeDate value called from the backend data.
let curTime = new Date(); // Thu Oct 27 2022 15:02:34 GMT+0900
const pwDate = new Date(pwChangeDate) // Thu Oct 20 2022 13:51:57 GMT+0900
At this time, when pwDate passes 90 days based on curTime, I want to display an alert saying "90 days have passed." and when 83 days have passed, "7 days left out of 90 days." I want to display an alert.
but if i use my code it doesn't work how can i fix it?
const pwChangeDate = cookie.get('pwChangeDate');
const pwDate = new Date(pwChangeDate)
if (curTime.getDate() >= pwDate.getDate() - 90) {
alert('90 days have passed.')
}
if (curTime.getDate() >= pwDate.getDate() - 7) {
alert('7 days left out of 90 days..')
}
you can get the diff between current data and pwDate in days like this:
const pwDate = new Date('Thu Oct 20 2022 13:51:57 GMT+0900');
const diff = Math.floor((new Date - pwDate)/1000/60/60/24);
console.log(diff)
If you want to calcuate the days difference between pwDate and curTime, you can calculate like this.
Math.floor((pwDate.getTime() - curTime.getTime()) / (1000 * 60 * 60 * 24));
getTime() method returns a time value as milliseconds.
1000 * 60 * 60 * 24 is milliseconds per day.
OR
Using some library. (date-and-time)

How to rotate 360° day/night cycle image based on the real time of the day?

the day/night circle example:
https://i.ibb.co/fFskJ28/exmp.png
I want the day/night circle image (example in the link above) to rotate based on the real time (+1 UTC, no winter/summer time adjustments). The times you see in the image are already based on the +1 UTC time.
I already achieved to make this work in my game maker project, but now I also want this day/night circle image on my website so visitors can also see the day/night cycle live there.
I have already the working code written in GML, but now I want it in PHP/Javascript, and I know not much about Javascript, but I guess it is a must to use if I want the day/night cycle image to rotate in live time.
So here's my working code written in GML:
//written in GML
// unix_timestamp([datetime])
//
// Returns a Unix timestamp for the current time
// or optionally given GameMaker datetime value.
{
var timezone = date_get_timezone();
date_set_timezone(timezone_utc);
if (argument_count > 0) {
var datetime = argument[0];
} else {
var datetime = date_current_datetime();
}
var timestamp = round(date_second_span(25569, datetime));
date_set_timezone(timezone);
return timestamp;
}
The following part is a bit messy but does all what I want, making the variables 'hour' and 'minute' equal to the real time hour/minute in my country (+1 UTC)
//written in GML
rtime = unix_timestamp();
//removing all seconds from 1 jan 1970 00:00 - 1 jan 2019 00:00 (UTC+1 Amsterdam)
rtime2 = (rtime - 1546300800+3600);//the +3600 is meant to add 1 hour to
equalise to the UTC+1 time
//remove all remaining days
{
while (rtime2 >= 86400)
{
rtime2 -= 86400;
}
}
dtime = unix_timestamp();
//removing all seconds from 1 jan 1970 00:00 - 1 jan 2019 00:00 (UTC+1 Amsterdam)
dtime2 = (dtime - 1546300800+3600);//the +3600 is meant to add 1 hour to
equalise to the UTC+1 time
//number of days from 1 jan 2019 00:00
day_unf = (dtime2 / 86400);
day = (floor(day_unf) + 1);
//count all remaining hours
hour_unf = (rtime2 / 3600);
hour = (floor(hour_unf))
qtime = unix_timestamp();
//removing all seconds from 1 jan 1970 00:00 - 1 jan 2019 00:00 (UTC+1 Amsterdam)
qtime2 = (qtime - 1546300800+3600);//the +3600 is meant to add 1 hour to
equalise to the UTC+1 time
//remove all remaining days
{
while (qtime2 >= 86400)
{
qtime2 -= 86400;
}
}
removar = (hour * 60)
//count all remaining minutes
minute_unf = (qtime2 / 60);
minute_unf2 = (minute_unf - removar);
minute = (floor(minute_unf2))
xtime = unix_timestamp();
//removing all seconds from 1 jan 1970 00:00 - 1 jan 2019 00:00 (UTC+1 Amsterdam)
xtime2 = (xtime - 1546300800+3600);//the +3600 is meant to add 1 hour to
equalise to the UTC+1 time
//remove all remaining days
{
while (xtime2 >= 86400)
{
xtime2 -= 86400;
}
}
rem = (minute * 60);
rem2_unf = (hour * 3600);
xtime3 = (xtime2 - rem);
second = (xtime3 - rem2_unf);
if hour == 24{
hour = 0;
}
//written in GML
pre1 = 15 * hour;
pre2 = 0.25 * minute;
pre3 = pre1 + pre2;
cycle_angle = pre3;
//cycle_angle is always a number between 0-360 and is used below to draw the day/night circle image in the right rotation degree.
draw_sprite_ext(spr_day_night_cycle,0,960,80,image_xscale*1.5,image_yscale*1.5,cycle_angle,c_white,image_alpha);
Since the image is a circle of 360° degree, and it represents the time of one whole day of 24 hour, every passing hour is equal to 15° rotating, and every minute is equal to 0.25° rotating.
When the time is 18:30 for example, it rotates 277,5° (15 * 18 + 30 * 0.25) to represents that it is will day but the sunset is close.
So my questions are:
Question 1: How can I make the variables 'hour' and 'minute' in PHP/Javascript based on the real time hour/minute in my country (+1UTC, no winter/summer time adjustment needed)
Question 2: If I succeed in question 1, how can I rotate my day/night circle image on the website based on the 'hour' and 'minute' variables like I did in GML? (see GML example below)
//written in GML
pre1 = 15 * hour;
pre2 = 0.25 * minute;
pre3 = pre1 + pre2;
cycle_angle = pre3;
//cycle_angle is always a number between 0-360 and is used below to draw the day/night circle image in the right rotation degree.
draw_sprite_ext(spr_day_night_cycle,0,960,80,image_xscale*1.5,image_yscale*1.5,cycle_angle,c_white,image_alpha);
In PHP:
$date = new \DateTime();
$date->format('Y-m-d H:i:s'); //returns a string
There's many more functions in the DateTime object; read about it here
In JS:
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
You can then use those values and use CSS to rotate an image from 0-360 degrees using transform: rotate(360deg);

16 digit timestamp to Javascript date format

I am getting a 16-digit Timestamp from the server.
For example : I got "1485157072432000" from the server and when I use the time stamp converter it is showing as Wed Oct 03 49032 04:43:52 GMT+0530 (India Standard Time)
However,I am getting the exact time when I remove the last 3-digits from the 16-digit number. But I'm getting 16 digit Timestamp from server. What is the way to get exact time from 16-digit Timestamp??
Since you need the microseconds precision I guess that the only solution is to use a custom object where you store all the information you need.
I would suggest you to use something like this utility developed by the GitHub user kamikane.
In particular you need the parse function that he developed:
function parse(nano) {
var ms = nano / 1000;
var ss = ms / 1000;
var mm = ss / 60;
var hh = mm / 60;
var dd = hh / 24;
var microseconds = Math.round((ms % 1) * 1000);
var milliseconds = Math.floor(ms % 1000);
var seconds = Math.floor(ss % 60);
var minutes = Math.floor(mm % 60);
var hours = Math.floor(hh % 24);
var days = Math.floor(dd);
return { microseconds: microseconds, milliseconds: milliseconds, seconds: seconds, minutes: minutes, hours: hours, days: days, toString: toString };
};
Example usage:
parse(1485157072432010);
{ microseconds: 10, milliseconds: 432, seconds: 52, minutes: 37, hours: 7, days: 17189 }

Rounding time in JavaScript

I am trying to figure out a formula to calculate the fraction of an hour. I want to break up an hour into six-minute intervals. This means I would have the following table:
Input Output
----- ------
5 hrs 0 mins 5.0
5 hrs 1 min 5.0
5 hrs 2 mins 5.0
5 hrs 3 mins 5.0
5 hrs 4 mins 5.1
5 hrs 5 mins 5.1
5 hrs 6 mins 5.1
5 hrs 7 mins 5.1
5 hrs 8 mins 5.1
5 hrs 9 mins 5.1
5 hrs 10 mins 5.2
5 hrs 11 mins 5.2
5 hrs 12 mins 5.2
5 hrs 13 mins 5.2
5 hrs 14 mins 5.2
5 hrs 15 mins 5.2
5 hrs 16 mins 5.3
...
Currently, I have the following:
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var fraction = Math.floor(m / 60);
var result = h + '.' + fraction;
The fraction part is not working right. There is something about rounding, etc. that I'm not sure how to handle in this situation. I would be thankful for anyone that can help me with this problem.
Thanks!
You have to apply a little logic to properly format a duration with javascript. The logic is needed to consider the final minutes in an hour. The following should work for you:
function FormatDuration(duration) {
// Retrieve the hours and minutes
var hrs = duration.getHours();
var mins = duration.getMinutes();
// Convert the minutes to a fraction of an hour.
var tenths = ((mins / 60).toFixed(1) * 10);
if (tenths === 10) {
tenths = 0;
hrs = hrs + 1;
}
return hrs + '.' + tenths;
}
Dividing by 60 will always give you a value between 0 and 1, meaning floor() will always return 0 (unless m is equal to 60). You can divide by 6 instead:
var fraction = Math.floor(m / 6);
Tests:
Math.floor(6 / 6); // 1
Math.floor(3 / 6); // 0
Math.floor(30 / 6); // 5
Math.floor(59 / 6); // 9
I don't know if this is correct but this seems to match your numbers:
var fraction = Math.round(m/6.1) / 10;
I used 6.1 to push the numbers down one as with just 6 3 minutes becomes .1 instead of 4 minutes becoming .1
This leads to 58 and 59 minutes being a whole 1.0 which would bump up your hours to the next. Not sure if that is what you want as your samples only go up to 16 minutes.
I came up with this by playing in excel with the numbers from 0 to 59. It's a great way to test these types of issues.

JDE/Julian Time: How to format julian time stamp number

In my system, time stamps are returned using the old IBM julian format.
For example:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight) is returned 01.
12 o'clock 22 minutes and 15 seconds AM is returned 2215.
1 o'clock 22 minutes and 15 seconds AM is returned 12215.
7 o'clock 45 minutes and 1 seconds AM is returned 74501.
7 o'clock 22 minutes and 15 seconds PM is returned 192215.
I need a regex expression to put these into the format of:
12 o'clock 0 minutes and 1 seconds AM (1 sec after midnight): 00:00.01
12 o'clock 22 minutes and 15 seconds AM: 00:22.15
1 o'clock 22 minutes and 15 seconds AM: 01:22.15
7 o'clock 45 minutes and 1 seconds AM: 7:45.01
7 o'clock 22 minutes and 15 seconds PM: 19:22.15
Any help is appreciated.
SOLUTION
Thanks to MikeM, here is the solution:
//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';
time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
function ( all, hr1, hr2, min, sec ) {
return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
}
);
The following works with your examples, though I haven't tested it beyond that
//var time = '01';
//var time = '2215';
//var time = '12215';
//var time = '74501';
var time = '192215';
time = time.replace( /^(?:(?:(\d)?(\d))?(\d\d))?(\d\d)$/,
function ( all, hr1, hr2, min, sec ) {
return (hr1 || '0') + (hr2 || '0') + ':' + (min || '00') + '.' + sec;
}
);
Although it gives 07:45.01 not 7:45.01, so as to be in keeping with 01:22.15.
I'll give you a clue:
Convert returned value to a number.
num % 100 is the seconds.
(num / 100) % 100 is the minutes.
(num / 10000) is the hours.
If the hours is less than 12, use AM
If the hours is 12 or more, use PM and further, if its 13 or more, subtract 12.
Another way to do it is to treat it as a string. But then you have to add enough leading zeros to get to length 6 and then break it into 2 character bits and convert each to an 'int' and that's way more work than just mod-ing by 100 and diving by 100 and 10,000.
There should never be a value in those two digit sections greater than 59.
Note
#radi8 noticed something I left out. I should have noted that the "/" (division) in the above algorithm has to be integer arithmetic for it to work right. Some programming languages offer integer arithmetic. JavaScript does not.
Since JavaScript uses floating point arithmetic, he subtracts off the number of seconds before dividing. Then a similar subtraction of the number of minutes fixes the next step.
You could also use Math.floor() after dividing to accomplish the same thing (since these are positive numbers).
Here is OP's code modified to do that:
$(function () {
var val1 = 41215,hr=0,min=0,sec=0;
sec = val1%100;
val1 = Math.floor(val1 / 100);
min = val1%100;
hr = Math.floor(val1 / 100);
// format the result. This example could show 1:1:1 instead of 01:01:01
tst2 = hr.toString()+':'+min.toString()+'.'+sec.toString();
alert(tst2.toString());
});

Categories