Count days, hours and minutes to a specific date with javascript - javascript

I have this script that counts the days, hours and minutes from the time at the moment until May 8 at 23:59:59 - 1 second before May 9, and displays it as a counter with FlipClock, but for some reason it is missing 2 days and 1 hour. From today March 23 at 10:37 (24 hr clock) there are 48 days, 13 hours, 23 minutes hours, but my clock shows 46 days, 12 hours, 23 minutes.
Should be very simple, but I cannot figure out where the 2 days and 1 hour are gone missing.
I have this javascript:
// Grab the current date
var currentDate = new Date();
// Set the date to May 8
var futureDate = new Date(2016, 04, 08, 23, 59, 59);
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
var clock = new FlipClock($('.clock'), diff, {
clockFace: 'DailyCounter',
countdown: true,
showSeconds: false,
language: 'da'
});

If I have to do anything with times, I usually use momentjs, it does not mean you have to, but I know it works and it works correctly enough for my criterias.
Duration format is not part of moment js, it is a feature. You could roll one yourself, but John Madhavan-Reese has already done this.
var futureDate = moment(new Date(2016, 04, 08, 23, 59, 59));
setInterval(function() {
var ms = moment().diff(moment(futureDate));
var duration = moment.duration(ms).format("yy-MM-dd hh:mm:ss");
$("#time").text(duration);
}, 200);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
<span id="time"><span>

Related

How to correctly calculate difference in days between 2 dates with DST change?

I have a task to get days difference between 2 dates. My solution is like here https://stackoverflow.com/a/543152/3917754 just I use Math.ceil instead of Math.round - cause 1 day and something is more than 1 day.
It was fine until I got dates between DST change. For example:
In my timezone DST change was on 30 Oct.
So when I'm trying to find days diff between dates 20 Oct and 10 Nov in result I get 23 instead of 22.
There are solution how to identify DST date but is it good solution to add/substract 1 day if date is/isn't dst?
function datediff(toDate, fromDate) {
const millisecondsPerDay = 1000 * 60 * 60 * 24; // milliseconds in day
fromDate.setHours(0, 0, 0, 0); // Start just after midnight
toDate.setHours(23, 59, 59, 999); // End just before midnight
const millisBetween = toDate.getTime() - fromDate.getTime();
var days = Math.ceil(millisBetween / millisecondsPerDay);
return days;
}
var startDate = new Date('2022-10-20');
var endDate = new Date('2022-11-10');
console.log('From date: ', startDate);
console.log('To date: ', endDate);
console.log(datediff(endDate, startDate));

FormatDistanceToNowStrict is displaying wrong day

console.log(dateFns.formatDistanceToNowStrict(new Date(2021,08,23),
{ unit:'day'})) // returns 87 days when it should be 58 days
console.log(dateFns.format(new Date(), 'dd.MM.yyyy')) //displays the correct current date
I don't know why it is displaying 87 days on my discord.js bot instead of 59 days(That's like a whole month away, don't think it's a timezone issue).
Any idea what could be wrong with it, I'm trying to get time between now and another date and I'm using Date Fns, The roundings don't work either
Your problem is fairly simple. The month in the new Date() constructor is 0-indexed. That means if you want to set 08 (August) as your month you actually have to pass 07 since January is 00 and not 01.
// With 08 as month ❌
const difference1 = new Date(2021, 08, 23) - new Date();
console.log(`08: ${difference1 / 1000 / 60 / 60 / 24} days`);
// With 07 as month ✔️
const difference2 = new Date(2021, 07, 23) - new Date();
console.log(`07: ${difference2 / 1000 / 60 / 60 / 24} days`);
Okay figure it out.
console.log(dateFns.formatDistanceToNowStrict(new Date(2021,08,23),
{ unit:'day'}))
Change the date format to
console.log(dateFns.formatDistanceToNowStrict(new Date('2021-08-23'),
{ unit:'day'}))

How to Set a Countdown with time zone?

How to Set a Countdown with time zone via countdown.js?
(https://www.npmjs.com/package/countdown)
I need:
begin at noon Eastern Standard Time on Wednesday November 22, 2017
today is 2017.11(Nov).01
and I used this code for checking tommorrow
const countdown = require('countdown');
var aaa = countdown( new Date(2017, 11, 2) ).toString();
console.log(aaa)
But my Output is:
1 month, 19 hours, 21 minutes and 11 seconds
That Output is incorrect maybe because I'm in uae Now
You can utilize moment timezone to get the time zone with countdown.js: https://momentjs.com/timezone/
Kind of like:
var tz = moment.tz.guess(),
date = moment.tz('2017-11-22', tz),
now = new Date(),
diff = (date.valueOf() / 1000) - (now.getTime() / 1000)

Setting a set date with javascript

Using a countdown plugin but I think I'm setting the date and time wrong.
Code:
var clock;
$(document).ready(function() {
// Grab the current date
var currentDate = new Date();
// Set some date in the future. In this case, it's always Jan 1
var futureDate = new Date(2016,10,27, 10,00,00);
// Calculate the difference in seconds between the future and current date
var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;
// Instantiate a coutdown FlipClock
clock = $('.clock').FlipClock(diff, {
clockFace: 'DailyCounter',
countdown: true
});
});
I'm attempting this:
var futureDate = new Date(2016,10,27, 10,00,00);
Which is 27th October 2016 at 10am
Coding up 52 days though so I must be doing something wrong
Which is 27th October 2016 at 10am
That's where you're going wrong. Months in JavaScript are 0-indexed (January is 0, December is 11), the 10th month is actually November.
var futureDate = new Date(2016,9,27,10,00,00);

Javascript adding days to a Date

Hi I am trying to create a variable today that is the current date today. I am trying to add 106 days to it which works successfully. Then I am trying to create a second variable today2 and subtract 31 days from the 'today' variable (current date + 106 -31). This part is not working. This is what it is giving me...
Thu Mar 28 11:52:21 EDT 2013
Tue Nov 27 11:52:21 EST 2012
The second line is not 31 days before the first line. Can someone help me correct this?
Feel free to play with my jsfiddle http://jsfiddle.net/fjhxW/
<div id="current"></div>
<div id="current2"></div>
<div id="current3"></div>
var today = new Date();
var today2 = new Date();
today.setDate(today.getDate() + 106);
today2.setDate(today.getDate() - 31);
var dd = today.getDate();
var mm = today.getMonth(); //January is 0!
var yy = today.getFullYear();
document.getElementById('current').innerHTML = today;
document.getElementById('current2').innerHTML = today2;
it's Xmas time so I give the answer just to copy/paste:
var oneDay = 24 * 60 * 60 * 1000, // 24h
today = new Date().getTime(), // in ms
firstDate,
secondDate;
firstDate = new Date(today + 106 * oneDay);
secondDate = new Date(firstDate.getTime() - 31 * oneDay);
try datejs:
Date.parse('t - 31 d'); // today - 31 days
Date.today().add(106).days().add(-31).days();
You cannot pass a negative number to setDate. setDate is used to set the date to set the absolute day, not relative days.
From the docs:
If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month.
A mathemathical solution:
Add 75 days to your current day (106 - 31), then add 31 days to that date. Change the order in what you are showing both dates on your code.
Why go forward and backward when you can always go forward?

Categories

Resources