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 }
Related
I would like to put this timer to show how many hours are left for the day to end in Brasilia time, Brazil, I am not able to form something with gethours
{% assign productTags = product.tags %}
{% if productTags contains 'contador' %}
<script>
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.getHours();
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
window.onload = function () {
var fiveMinutes = 60 * 29.5,
display = document.querySelector('#time');
startTimer(fiveMinutes, display);
};
Can I do this with this code?
One way to approach this is to determine today and tomorrow's calendar date using the much loved Date API.
You can get today's date in milliseconds since epoch using:
const now = new Date()
// Sun Dec 11 2022 13:21:03 GMT-XXXX (Your Time Zone)
You can then get that in milliseconds since Epoch
const nowMsSinceEpoch = now.getTime()
// 1670793708603
This is the basis for the solution. If you can find today's date in milliseconds and tomorrow at 00:00, you can compare the two:
let tomorrow = new Date()
tomorrow.setDate(now.getDate() + 1)
tomorow.setHours(0,0,0,0)
// Mon Dec 12 2022 00:00:00 GMT-XXXX (Your Time Zone)
At this stage, you're ready to get the difference between tomorrow and today:
const differenceMs = tomorrow.getTime() - now.getTime()
By subtracting one timestamp in relation to epoch from another that lives in relation to epoch, you now have a difference, and something you can now calculate the hours, with some accuracy. This is now just a matter of arithmetic. You'll need to determine how many milliseconds lie within an hour, and you you will seek what way you wish to round the number.
I am getting the elapsed time in minutes, hours and days, between two dates, a past date and the current one, I already get this data, but I want this data to change as the minutes, days and hours increase. For example, when I get to 60 minutes, the time changes to 1 hour and the minutes go to 0, when 24 hours go by, these hours change to a day and the hours go back to 0, and so on, the data I get keeps increasing , how can I do this?
const calculateDate = () => {
const date = new Date('Sun Sep 01 2022 01:32:06 GMT-0500');
const currentDate = new Date();
const minutes= Math.floor((currentDate.getTime() - date.getTime()) / 1000 / 60);
const hours= Math.floor((currentDate.getTime() - date.getTime()) / 1000 / (3600));
const days= Math.floor((currentDate.getTime() - date.getTime()) / (1000*60*60*24));
}
With this, get the minutes, hours and days, but how would you update so that when you reach 60 minutes it goes to one hour and 24 hours to one day?
The JavaScript Date object has built in functions for what you want to do.
var now = new Date()
var h = now.getHours()
var m = now.getMinutes()
var s = now.getSeconds()
The new Date created in above example is set to the time it was created.
You can get the current time using the Date object itself:
var current = Date()
With your method you always see the full duration just in a different unit.
You have to use the modulo operator to get only the "missing part" (the remainder of the division) to the next unit:
const date = new Date('Sun Sep 01 2022 01:32:06 GMT-0500');
const currentDate = new Date();
const dateDiff = (currentDate.getTime() - date.getTime()) / 1000;
const seconds = Math.floor(dateDiff) % 60;
const minutes = Math.floor(dateDiff / 60) % 60;
const hours = Math.floor(dateDiff / (60 * 60)) % 24;
const days = Math.floor(dateDiff / (60 * 60 * 24));
I have a Webhook listener that receives a unix timestamp value.
This timestamp is my END time.
I would like to use the current unix timestamp and compare what HH:MM:SS are left until the end time.
I was reading this post: How can i find HH:MM:SS difference between two UNIX timestamps?
and think it is very similar to my needs but needs a little tweaking.
Example:
Current time = unix now time
End time = unix time
= How many HH:MM:SS remain before the time has ended
I was trying;
function timeDiff(EpochTime) {
let msec = (new Date()).valueOf() - EpochTime * 1000;
const hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
const mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
const ss = Math.floor(msec / 1000);
msec -= ss * 1000;
return `${mm}m ${ss}s`;
}
Thanks
Magik
GNU awk is an option:
awk -v etim="2021 02 08 16 27 00" '{ print strftime("%c",mktime(etim)-strftime("%s"),1) }' <<< /dev/null
Pass the date/time as etim and the use GNU awk's strftime and mktime functions to print the difference in the passed date and now in the locale format. Change %c to what ever format is required.
I want to create timestamp list grouped by X minutes.
For instance I have min data value and max date timestamp value.
minDate = 1524548466000; // Tuesday, April 24, 2018 5:41:06 AM
maxDate = 1524577986000; // Tuesday, April 24, 2018 1:53:06 PM
So I want to create timestamp array start with Tuesday, April 24, 2018 5:40:0 AM to Tuesday,April 24, 2018, 1:55:00 PM using javascript.
I want this output:
[1524548400000,1524548700000,1524549000000,...1524578100000]
How can I do this?
Here is my solution. I just count second and compare with maxDate.
var minDate = new Date(1524548466000); // Tuesday, April 24, 2018 5:41:06 AM
var maxDate = new Date(1524577986000); // Tuesday, April 24, 2018 1:53:06 PM
var listTimestamp = [];
while (minDate.getTime() !== maxDate.getTime()) {
minDate.setSeconds(minDate.getSeconds() + 1);
listTimestamp.push(minDate.getTime());
}
console.log(listTimestamp);
Here the function you needed
function timestampGrouping(minDate, maxDate, x){
var gap = x * 60000 //60000 = 1minute;
minDate = Math.floor(minDate/gap)*gap;
maxDate = Math.ceil(maxDate/gap)*gap;
var arr = [];
while(minDate <= maxDate){
arr.push(minDate);
minDate += gap;
}
return arr;
}
var groupingArr = timestampGrouping(1524548466000, 1524577986000, 5);
console.log(groupingArr);
Math.floor and Math.ceil is to find the nearest gap value you required, and just loop thought the value to get all the minute gap you want.
minDate, maxDate and gap value can be anything you wish to.
Hope this is what you required.
You could divide the given Unix Time (Epoch Time) and divide it by a product of wanted minutes, seconds and 1000 for miliseconds.
5 minutes * 60 seconds * 1000 milliseconds = 300000
For moving events in the same slot use
Math.floor(time / 300000)
if duration is 5 min then you can set duration variable as follows.
var duration = 5 * 60 * 1000;
endTime = endTime - endTime % duration;
startTime = startTime - startTime % duration;
for(var i=startTime;i<endTime;i+=duration){
arr.push(i);
}
I want to set GMT+5:30 as my timezone in jquery countdown.
Start Time for countdown is 'Thu May 20 16:00:00 IST 2010'
End Time is 'Thu May 20 17:00:00 IST 2010' as value.
+330 is my timezone given in minutes.
But my countdown starts from 00:35:00.
I would have expected the countdown to start from 01:00:00
Not sure why this is discrepancy is there.
<script type="text/javascript">
$(function () {
var endTime = '#{myBean.getCountDownDate()}';
$('#defaultCountdown').countdown({
until: endTime, format: 'HMS',
timezone: +330,
compact: true, description: '#{myBean.getCountDownDate()}'});
});
</script>
When using the until parameter the countdown plugin counts down until that time.
This will run for one hour using the correct offset.
$('#countdown').countdown({
until: $.countdown.UTCDate(+330, 2010, 6-1, 20, 17),
format: 'HMS',
compact: true
});
Since 2010:06:20:17 has already passed it will display 00:00:00.
I would bet the reason you got 00:35:00 in your countdown is that you were looking at it around 2010:06:20:16:25.
What happens when you change your End Time format to 'Thu, 20 May 2010 17:00:00 IST'?
-edit-
It looks like you're not supposed to pass the date value to until as a String. You can pass in a Date to specify the exact date/time, but a string is only supposed to be used as a time offset, which is why you always get the same amount of time remaining when you refresh.
I couldn't get Date to convert the string with the 'IST' time zone, so I ended up using 'GMT+05:30'. I also put the timezone offset in terms of hours instead of minutes.
<script type="text/javascript">
$(function () {
var endTime = "Tue, 29 Jun 2010 12:00:00 GMT+0530";
$('#defaultCountdown').countdown({
until: new Date(endTime), format: 'HMS',
timezone: +5.5,
compact: true, description: endTime.toString()});
});
</script>
to set timer with eastern timezone
<script>
setInterval(function(){
var timeZone = "America/New_York";
var TimerEndDate = "Nov 25 2022";
var endHour = 23;
var endMinute = 59;
var endSeconds = 59;
//for starting time for timer
//bydefault set to america current time
var nowDate = new Date();
var nowTimeZone = convertTZ(nowDate, timeZone);
var now = new Date(nowTimeZone.getFullYear(),nowTimeZone.getMonth(),nowTimeZone.getDate());
now.setHours(nowTimeZone.getHours(), nowTimeZone.getMinutes(), nowTimeZone.getSeconds());
var endDate = new Date(TimerEndDate+" "+endHour+":"+endMinute+":"+endSeconds);
var endDateTime = convertTZ(endDate, timeZone);
var future = new Date(endDateTime.getFullYear(),endDateTime.getMonth(),endDateTime.getDate());
future.setHours(endHour, endMinute, endSeconds);
var difference = Math.floor((future - now) / 1000);
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
$("#seconds").text(seconds + "s");
$("#minutes").text(minutes + "m");
$("#hours").text(hours + "h");
$("#days").text(days + "d");
}, 1000);
function convertTZ(date, tzString) {
return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));
}
function fixIntegers(integer)
{
if (integer < 0)
integer = 0;
if (integer < 10)
return "0" + integer;
return "" + integer;
}
</script>
<span id="days"></span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>