I'm using the Scala Play framework and I have a form that lets a user search for records, they can then search using today's date and a time stamp 2016-12-17T09:26:47.676Z to create a search range. Note that I mean a 24 hour period, as a single day is usually defined, and not as a difference in a date definition.
I don't have any experience with Javascript, I'd like to compare today's date with this time stamp, calculate the difference in days, and then return the amount of whole days between the two time stamps e.g if the two timestamps had 48 hours difference then it would return 2 days.
I can declare the search time stamp in the script tag on the HTML page, but I would like to know how to call today's date, do some logic to calculate the difference between the dates DaysDifference, and then call this variable in a link on the page:
<a target="_blank" href="#baseLogsUrl/#/discover?_g=(time:(from:no#DaysDifference/d,to:now))&_a=(query:(query_string:(query:'#data._id')))">
You can compute the difference between the current date and a timestamp with the Date API like this: Date.now() - Date.parse('2016-12-17T09:26:47.676Z'). This will return the difference in milliseconds, so we can divide the answer by the number of milliseconds in a day to obtain the difference in days. Summarizing:
var MS_IN_A_DAY = 1000 * 60 * 60 * 24;
var difference = Date.now() - Date.parse('2016-12-17T09:26:47.676Z');
var daysDifference = Math.floor(difference/MS_IN_A_DAY);
Related
I am sending Json data via a REST Service to my client. This client should use the data to Display it.
My client uses JavaScript.
I am converting the date in the following way:
var from = new Date(myJsonDate.match(/\d+/)[0] * 1);
The JSON looks like this:
...="From":"\/Date(1450134000000)\/" ...
My problem is that the dates are correct in Germany but are off by one day in Brazil (e.g. showing Sunday instead of Monday in Brazil).
Does this code use time zones and calculates this accordingly?
How could I turn this off?
I want that the date is displayed exactly how i have sent it.
The operations with dates in JavaScript have a time zone variation in which the client machine is configured.
Right opportunity had to fix a function that showed difference between dates and nobody knew because. When you instance a date, the return her appears as: “Thu Feb 14 2008 08:41:27 GMT-0300 (Official Hour of Brazil)”
Note that in date has the GMT (Greenwich Mean Time) that indicates in which time zone the date is configured.
I’ll show as avoid the difference of time caused by this in operations with date. To this we have create a function that convert the date always to the time zone that if wait.
var calculateTimeZone = function(date, offset) {
var miliseconds_with_utc = date.getTime() + (date.getTimezoneOffset() * 60000);
return new Date(miliseconds_with_utc + (3600000 * offset));
}
Note that in the line 3, we invoke the method getTime() that convert the local moment of date to a number represented by miliseconds since January 1st, 1970 (Unix Epoch). We get the current time zone that is set in browser by method geTimezoneOffset() of API the date in JavaScript and we multiply by miliseconds of time of a hour. We add then the two values.
Why a hour?
Why this is the time that represents each time zone. By default this method return this time zone in minutes, by this the convertion in hour is necessary.
For to arrive this number 60000 you have that remember that 1 second have 1000 miliseconds and which 1 minute have 60 seconds, then converting minutes for miliseconds we multiply 60*1000 = 60000.
This moment we have the UTC (Coordinated Universal Time) represented by variable “utc” by sum of local moment the time zone in miliseconds.
We need now get a date starting this UTC added with the time zone of destiny, how by example a date expressed in time zone +5 transforming in time zone of brazil (Hour of Brazilian).
Note that in line 5 we got an offset (Time Zone Representation) in hour and converting to miliseconds. Remember that here 1 second have 1000 miliseconds and which 1 hour have 3600 seconds, then convert hour in miliseconds should multiply 1000 * 3600 = 3600000.
We add this result with the value of variable “utc” and we got the moment to the time zone wanted. Thenceforth we create a new date with based in long appropriate and return this new date.
In this way we can maintain of integrity desired in application when we need expressed a date in right time zone.
Does this code use time zones and calculates this accordingly?
No. Passing a number to the Date constructor is interpreted as a time value, i.e. milliseconds since 1970-01-01T00:00:00Z. Regardless of the settings of the client, it will create a Date for exactly the same instant in time.
However, by default, Date.prototype.toString uses the host system settings to apply an offset to the displayed values as "local" time.
How could I turn this off?
Modify the script engine. It's part of the ECMAScript standard so any implementation that doesn't do it is non–compliant.
I want that the date is displayed exactly how i have sent it.
Either:
Send it as a plain string, not as a date
Also send the time zone offset of the source so you can apply it at the other end to keep the date the same.
ECMAScript offsets have an opposite sense to most standards, they're -ve for east and +ve for west, so to get a Date with local settings that has the same as the source system:
var d = new Date(timevalue);
d.setMinutes(d.getMinutes() + d.getTimezoneOffset() - sourceTimezoneOffset);
Where sourceTimezoneOffset is the offset of the source system in minutes, +ve for west and -ve for east.
Usually dates related to a specific time zone, so as pointed out, the date in one place might be different to the date in another place at the same instant in time.
If you are not doing any modifications in dates when sending it from server side, the date will be in the timezone where the server is hosted.
So, if your server is hosted in Germany, dates will be in Germany's timezone.
There would be 2 ways to solve this:
Send dates to client in user-timezone from server in the response.
Make adjustments in your client application to implement appropriate
date conversion.
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/
I need the epoch time in days. I've seen posts on how to translate it to date but none in days. I'm pretty bad with epoch time...how could I get this?
I need the epoch time in days
I'll interpret that you want the number of days since the epoch. The epoch itself is day zero (or the start of day 1, however you want to view it).
At the heart of a javascript Date object is a number of milliseconds since 1970-01-01T00:00:00Z. So to get the number of days from then to now you simply get the current time value and divide it by the number of milliseconds in one day:
var now = new Date();
var fullDaysSinceEpoch = Math.floor(now/8.64e7);
For 2012-10-05 you should get 15618. Not sure if it allows for leap seconds and such, but it should be close enough (within a few seconds) if the system clock is accurate.
It is only when reading values of a Date object (such as getHours() and toString()) that the timezone offset is applied to give local times.
I am creating a Javascript based date filter to filter files, which has date_created metadata.
I wish to create that date filter as a range slider with max/min (are static) and current selection. For this going to use jQuery UI slider component. Visible output has to be always in a format dd/mm/yyyy and I will show it for min/max and from/to range selectors.
While developing, I came to a question, which format I have to use on a hidden part to make steps inside the slider. Each step has to be 1 day. I decided to try with unix on hidden side. So I can easily convert unix into dd/mm/yyyy with my Javascript function:
function unixToDate(timestamp){
var date = new Date(timestamp * 1000);
var d = date.getDate(),
m = date.getMonth() + 1,
y = date.getFullYear();
return d + "/" + m + "/" + y;
}
And this function works well for me, however if step == 1day, I need 1 day value in unix to make addition (+) and subtraction (-) when user moves slider.
So which is that value of 1 day in unix, which I can add or subtract to when changing range?
Or any other alternatives to make date filter as slider…
One day is equal to currentTimeInUnixTime+(60*60*24)
That is because Unix time is simply the seconds since the beginning of epoch. 60 seconds make one minute. 60 minutes make an hour. And 24 hours make a day. Multiply them all, and add them to the current Unix time you have to get the Unix time for the next day.
I am pulling a row from a mysql database which has a time stamp, like this: 2010-10-10 16:56:23
I need to compare this time stamp to the current time from the date object, and see if 30 minutes has passed.
thanks!
How about:
(new Date() - Date.parse('2010-10-10 16:56:23')) >= 1000 * 60 * 30 // (current date - date) >= 30 minutes
I'm not sure about the whole time zone thing though.. if the timezones between the server and user are different you may need to mess around with that.
2010-10-10 16:56:23
make it 20101010165623
get current time also in this format
Just compare bot as integer itself.
You got it!.
Or
mysql have datediff() function which will help you.
http://www.w3schools.com/SQl/func_datediff_mysql.asp
in the example , your case second paramtere will be now()
Use DATE_ADD function and cast date type to your string
NOW() > DATE_ADD(CAST('2010-10-10 16:56:23' AS date), INTERVAL 30 MINUTE)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast