Subtract two date variables and get in terms of days in javascript - javascript

Here is my code.
var today = new Date();
var reqDate = new Date(today.getFullYear(),today.getMonth()-3, today.getDate());
var day = today-reqDate;
I want the 'day' should be something around 90; but it gives as some long integer.

The long integer is the number of milliseconds since midnight Jan 1, 1970. So in order to get the number of days you need to divide it. Code below:
var days = day/(1000*60*60*24);

You have got value in day variable as milliseconds, so divide it by 1000*60*60*24 to get day count.
Another thing, it will be a decimal value.
So you have to discard fraction value using floor function.
var days = Math.floor(day/(1000*60*60*24));

Related

c# vs javascript: Datetime.Now.Ticks vs Date.now() returns different values

I thought with the c# call:
var x = DateTime.Now.Ticks
and with the Javascript call:
var x = Date.now()
I should be getting the same result.
But on c# I am getting: 637593006969672760
While Javascript returns: 1623750547564
(Those are not from the same day, but should be extermely close. However, both values differ by A LOT)
I thought both return the value of miliseconds since the first of jan 0:00 of 1970?
So why are both values so different?
And how can I translate the c# call to javascript?
For c# Ticks:
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 in the Gregorian calendar
For Javascript Date:
JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
Sources:
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=net-5.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
In C# to get the number of milliseconds since some point in time (eg 1/1/1970) you could use:
var datum = new DateTime(1970,1,1);
var msSinceDatum = DateTime.Now.Subtract(datum).TotalMilliseconds;
Having run that a few seconds ago it gave the answer 1623751729961.4617 whereas the DateTime.now() in javascript gave 1623751739058. Should be close enough for your needs.
Note you can also use DateTimeOffset.ToUnixTimeMilliseconds
var msSinceUnix = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Going the other way (C# ticks to javascript date) is a little more involved.
var ticks = 637593490842364954; // ticks retrieved a few moments ago
var ticksToMicrotime = ticks / 10000;
var epochMicrotimeDiff = Math.abs(new Date(0, 0, 1).setFullYear(1));
var date = new Date(ticksToMicrotime - epochMicrotimeDiff);
console.log(date)

Convert getFullYear() in seconds in Javascript

Are there any way to convert the GetFullYear() in real time seconds from the beginning of the current year? Instead of having a output of 2017, i have a dynamic seconds until the end of the year.
As well as obtaining the maximum value of the GetFullYear() converted in seconds to make some calculations.
Thanks in advance
var date1 = new Date("1/1/2017");
var date2 = new Date("2/2/2017");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));//returns 32
var time_in_seconds = diffDays*60*60;// calculate the date in seconds.
Are there any way to convert the GetFullYear() in real time seconds from the beginning of the current year?
To get the difference in milliseconds between two dates, just subtract them. To get ms from the start of the year, subtract now from a date for 1 Jan at 00:00:00, e.g.
function msSinceStartOfYear() {
var now = new Date()
return now - new Date(now.getFullYear(), 0);
}
console.log(msSinceStartOfYear());
As well as obtaining the maximum value of the GetFullYear() converted in seconds to make some calculations.
According to ECMA-262, the maximum time value is 9,007,199,254,740,992, which is approximately 285,616 years after the epoch of 1 Jan 1970, or the year 287,586. So that is the theoretical maximum value of getFullYear.
However:
new Date(9007199254740992)
returns null. The actual range is exactly 100,000,000 days, so to get the maximum value, multiply days by the number of milliseconds per day:
new Date(100000000 * 8.64e7).getUTCFullYear() // 275,760
converted to seconds:
new Date(Date.UTC(275760, 0)) / 1000 // 8,639,977,881,600 seconds
but I have no idea why you want to do that. I've used UTC to remove timezone differences. Using "local" timezone changes the values by the timezone offset.

Calculating Time Difference in Javascript

I am trying to find time differences. Difference is NaN. What should I do?
currentTime.format() = 2016-12-07T11:43:19+03:00
pws.lastDataTime = 2016-12-07T08:35:14.4126931+00:00
var difference= currentTime.format() - pws.lastDataTime;
currentTime.format() = 2016-12-07T11:43:19+03:00
currentTime.format is a function. You can't assign it's return value to something.
currentTime.format() - pws.lastDataTime
I don't think the format function returns a number, but instead a string or an object. If you subtract anything from them, they return NaN (not a number). You need to either convert both to milliseconds and subtract one from the other, or calculate the year, month, day, hour, second and millisecond separately.
I don't know what denomination you want, so I'll just show you how to find it in milliseconds.
If already you have a date or two, you can use date.getTime().
var stackOverflowLaunchDate = new Date(2008, 8, 15);
var today = new Date();
var diff = today.getTime() - stackOverflowLaunchDate.getTime(); // milliseconds since Stack Overflow was launched
If you don't have (and don't need) a date object, use Date.now() to get millisecondds since epoch
var start = Date.now();
// ... Some time later
var diff = Date.now() - start; // milliseconds since start

get time different (minutes) in javascript

How do I calculate the difference in minutes given two strings. For example say I have
11:00
11:30
But of course the second string could be 12:11 so I can't subtract just the minutes.
first use javascript to convert the strings to time, then subtract, then convert back to strings
like this:
x = new Date("1/1/01 11:00")
y = new Date("1/1/01 11:30")
// now y-x has difference in milliseconds
// (y-x)/1000 is difference in seconds, etc
The data 1/1/01 is just being used as a dummy value, but the one thing you might have to worry about is are the times on different days, if so you will have to use 1/2/01 for the second time. Unless of course you always know the times are in the same day, but if they can cross "midnight" then you have to adjust for that.
You may want to use http://momentjs.com/ which will take care of the details for you.
When looking for getting metrics such as date , hour , minutes, seconds from the date difference, it's a lot easier to use basic notations as listed here
var x = new Date(new Date().getTime() + 11.5*60*60000); // adds 11 hours - 30 minutes
var y = new Date(new Date().getTime() + 11*60*60000); // adds 11 hours
alert(x.getMinutes() - y.getMinutes()); // gives the difference = 30
Here's an example : https://jsfiddle.net/DinoMyte/157knmgn/

What does it mean by the number of dates in Javascript's date() function?

I have simple question about Javascript date() function. This is a snippet of code
var today = new Date(); // current date
var xmas = new Date(2011,12,25); // this xmas
alert(Math.round ( today / (1000*60*60*24) ) ); // transform into number of days
alert(Math.round ( xmas / (1000*60*60*24) ) ); // transform into number of days
The output is as the following, respectively:
15141 // today
15364 // xmas
I don't know what does it mean by these numbers? 15141 (or 14999) days compare to what?
I also made another simple calculation to find the number of days from now to this year's xmas, to see if the above calculation is correct:
var total_days = Math.round(xmas/(1000*60*60*24)) - Math.round(today/(1000*60*60*24));
alert( total_days );
It returns 223 (days), this is correct.
So turning back to the orignal question, what does it mean by those numbers?
Javascript time starts at 1 January 1970 00:00:00 UTC. So its 14999 days since then.
You could refer to http://www.w3schools.com/jsref/jsref_gettime.asp
When a dateObj is cast to int, the value would be equals to dateObj.getTime()
To Calculate the days, they often use a reference point. Here it is 01/01/1970, and its the same for java.
Infact goto http://www.timeanddate.com/date/duration.html
It calculates days between 2 dates,go there and verify your numbers

Categories

Resources