How to do calculation of this statement - javascript

how to do the calculation of this statement
var dateTo = new Date(2019,04,03,3,15,0);
var countdown = Math.round((dateTo.getTime() - new Date().getTime()) / 1000);

Is the value not what you expected?
Try to print the date and make sure the value is what you wanted:
var dateTo = new Date(2019,04,03,3,15,0);
console.log(dateTo);
Also it is better to use only 4 and 3 instead of "04" and "03".

is this what you're going for?
#// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
console.warn("val", days);

Related

Calculate time remaining between now and a precise hour of next day

What's the most concise, performant way to get in Javascript the minutes remaining between now, and the upcoming day at 01:00 (am)?
Then, once the current time is after 01:00, I start calculating the difference to the next.
in javascript, a specified date can be provided like this
var date1 = new Date('June 6, 2019 03:24:00');
or it can be specified like this
var date2 = new Date('2019-6-6T03:24:00');
javascript can natively subtract 2 dates
console.log(date1 - date2);
//expected 0;
using this method will output the difference in the dates in milliseconds,
to get minutes you'll want to divide the value by 60000;
so
var futureTime = new Date('2019-06-06T07:24:00');
//there must be a 0 infront of 1 digit numbers or it is an invalid date
var now = new Date();
var difference = (futureTime - now) / 60000;
//get minutes by dividing by 60000
//doing Date() with no arguments returns the current date
read about the javascript Date object here for more information
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
let now = new Date();
let next1am = new Date();
next1am.setHours(1, 0, 0, 0); // same as now, but at 01:00:00.000
if (next1am < now) next1am.setDate(next1am.getDate() + 1); // bump date if past
let millisecondDiff = next1am - now;
let minuteDiff = Math.floor(millisecondDiff / 1000 / 60);
you can you moment.js here
var current = new Date()
var end = new Date(start.getTime() + 3600*60)// end time to calculate diff
var minDiff = end - start; // in millisec
You can calculate by pure JavaScript:
let today = new Date();
let [y,M,d,h,m,s] = '2019-06-04 05:00:11'.split(/[- :]/);
let yourDate = new Date(y,parseInt(M)-1,d,h,parseInt(m)+30,s);
let diffMs = (yourDate - today);
let diffDays = Math.floor(diffMs / 86400000); // days
let diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
let diffMins = (diffDays * 24 * 60)
+ (diffHrs *60)
+ Math.round(((diffMs % 86400000) % 3600000) / 60000); // The overall result
// in minutes
In, addition avoid using the built–in parser for any non–standard format, e.g. in Safari new Date("2019-04-22 05:00:11") returns an invalid date. You really shouldn't even use if for standardized formats as you will still get unexpected results for some formats. Why does Date.parse give incorrect results?

Javascript countdown without Year

So I found this code on this site but there's one thing I want to change about it, but I can't seem to figure it out so I need some help. What I want to do is to remove the year in the date, so that the countdown corresponds the year that it is.
<script>
var end = new Date('04/19/2017 10:1 AM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById('countdown').innerHTML = 'EXPIRED!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById('countdown').innerHTML = days + 'days ';
document.getElementById('countdown').innerHTML += hours + 'hrs ';
document.getElementById('countdown').innerHTML += minutes + 'mins ';
document.getElementById('countdown').innerHTML += seconds + 'secs';
}
timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>
Credit for the code goes out to this guy
All you have to do is to create a new Date instance without any parameters. This will give you a Date object with the current datetime. But this is very insecure because your information will come from the browser (the client's machine). You should generate this on the server.
Whatever, if you don't know how to create that instance in Javascript, here you go:
var currentDatetime = new Date();
var currentYear = currentDatetime.getFullYear();
And then you can do something like this:
var end = new Date('04/19/' + currentYear + ' 10:1 AM');
You can explore what that Date class can do here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Date
You should not parse strings with the Date constructor, they should be manually parsed with either a small function or library.
Assuming you have a date object, the year can be set using setFullYear, so:
// Create a date for 25 March, 2006
var d = new Date(2006, 2, 25);
console.log(d.toString());
// Set to current year
d.setFullYear(new Date().getFullYear());
console.log(d.toString());
If the date doesn't exist in the new year, e.g. if the starting date was 29 February 2016, then it rolls over to the next day, 1 March 2017.
If you know the date parts, you can go the other way and create a date and set the month and date, e.g.
// Create a date for 30 June in the current year
var d = new Date();
d.setMonth(5,30);
// Zero the time component
d.setHours(0,0,0,0);
console.log(d.toString());

How to get time length from now back to the start of today (00h:00p:00s) in angularjs

How to get timelength from now back to the start of today (00h:00p:00s) in angularjs?
ex: now is 13:45. So timelength = 13*60 + 45 mins
There is no specificity in angular. Just use the Date object.
var date = new Date();
var timelength = date.getMinutes() + date.getHours() * 60;
Get a new JavaScript date object that represents the time now and then use the getHours and getMinutes functions to enable your calculation.
For example:
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeLength = hours*60 + minutes;
Converting a javascript date to a number gives you milliseconds since 1/1/1970 (UTC). You can correct for your time zone if you wish, then just take the modulus of the number of milliseconds in a day to get the number of milliseconds since midnight:
var dt = new Date();
var num = dt - dt.getTimezoneOffset() * 60000; // offset is in minutes
var sec = num / 1000; // seconds
var sinceMidnight = sec % (24 * 60 * 60); // seconds since midnight

Where do I change the date in this countdown function?

I just downloaded this countdown script (JavaScript) but I can't figure out how to change the date that the timer will countdown to.
Original Script:
$(function(){
var now = new Date();
// comment out the line below and change the date of your countdown here
var in30Days = new Date( now.getTime() + (30 * 24 * 60 * 60 * 1000) );
// year to countdown to
var countdownYear = in30Days.getFullYear();
// month to countdown to 0 = Jan, 1 = Feb, etc
var countdownMonth = in30Days.getMonth();
// day to countdown to
var countdownDay = in30Days.getDate();
var countdownDate = new Date( countdownYear, countdownMonth, countdownDay );
setupCountdownTimer( countdownDate );
spaceParallax();
hideIphoneBar();
$("[placeholder]").togglePlaceholder();
setupSignupForm();
});
Maybe countdownDate...
By default it is built with now + 30 days? You may change this.
Important part is :
setupCountdownTimer( countdownDate );
spaceParallax();
hideIphoneBar();
$("[placeholder]").togglePlaceholder();
setupSignupForm();
});
Your code itself telling how to do it:
// comment out the line below and change the date of your countdown here
var in30Days = new Date( now.getTime() + (30 * 24 * 60 * 60 * 1000) );
// year to countdown to
var countdownYear = in30Days.getFullYear();
// month to countdown to 0 = Jan, 1 = Feb, etc
var countdownMonth = in30Days.getMonth();
// day to countdown to
var countdownDay = in30Days.getDate();
Following line creates the countDownDate, pass the year,month and day to the function
var countdownDate = new Date( countdownYear, countdownMonth, countdownDay );
setupCountdownTimer( countdownDate );

Javascript Countdown doesn't work

Everytime I reload this page http://prince27.bplaced.net/Prince27-Website/html/slider_true.html, the countdown starts all over again. What did I do wrong?
I've set the endDate to 20 september 2012 and calculated the milliseconds between now & endDate.
var today = new Date();
var endDate = new Date("20/09/2012"); //set the date you want timer to end
var diffMs = (280800000); // milliseconds between now & endDate
var diffDays = Math.round(diffMs / 86400000); // days
var diffHrs = Math.round((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
var diffSecs = Math.floor((((diffMs % 86400000) % 3600000) % 60000) / 1000);
Full script file
You've hardcoded the difference:
var diffMs = (280800000); // milliseconds between now & endDate
Review your code, you have hardcoded value for diffMs and you never use today
var today = new Date();
var endDate = new Date("20/09/2012"); //set the date you want timer to end
var diffMs = (280800000); // milliseconds between now & endDate

Categories