How to compare MySQL timestamp - javascript

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

Related

Javascript format special time

I have from a software a output like this timevalue:
15606260000 s
I don't know what format this is. The value in minutes is approximately 30 minute
How can I convert this with javascript in hour:min:seconds?
I think this is a epoch time or so?
this looks like a unix timestamp value (count in seconds since 01/01/1970)
what you can do in javascript is:
let d = new Date(15606260000); console.log(d.getUTCHours() + ":"+ d.getUTCMinutes() +":"+ d.getUTCSeconds());
and you can take it from there
You can use this link for a proper documentation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
keep in mind that JS time is in milliseconds most of the times you need to multiply by 1000

javascript returning accurate date when time is 23:59:59.000

I'm returning a date from json and parsing it to a date format like so;
var date = new Date(parseInt(date.substr(6)));
The problem is the date field in the database is set to 23:59:59.000 for the time.
The above code returns the day after using date.getDate().
I'm assuming this is due to the time.
How can i return the accurate date with the time being set to 23:59:59.000
Cheers
Edit
Incase anyone comes across this i fixed it by using;
var utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
Assuming that the database is storing the UNIX Epoch (in secs), try multiplying the parseInt with * 1000. This will also set the miliseconds to 0.
let timeFromDB = parseInt(date.substr(6)) * 1000
let date = new Date(timeFromDB);
Date API
Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).
The question was somewhat unclear, but hope this helps.

Javascript comparing current date to a timestamp

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);

Time Zones in Json Date

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.

In Javascript / NodeJS return current date adjusting for timezone, then find UTC equivalent of user's time at 6am

Sorry if the title is a little convoluted. I'm bashing my head against the floor with times in NodeJS / Javascript. I can get the current UTC time like this:
var currentTime = Date.now();
I can get the current time for a user who is, for example, in the -3 timezone like this:
var offsetTime = Date.now() + (numTimeZone * 3600000);
But how do I get the local user time at, say, 6am, converted to UTC?
Practical application:
What I'm trying to do is create an auto-emailer which sends an email to a user at 6am in their local time. My server is in one timezone and they will be in another, so I'm trying to standardise it against UTC so every minute I can set my server to check the currentUTC time, then check what the user's 6am time is converted to UTC (local6am), and if the currentUTC > local6am then an email should be sent.
What's the best way to achieve this? Preferably without using a library if possible.
Utc to Local
moment.utc('2014-02-19 05:24:32 AM').toDate();
Local to utc
Read this documentation.
MomentJS is parsing the date as a locale date-time. If no hour is given, it is assuming midnight.
Then, you convert it to UTC, so it is shifted, according to your local time, forward or backwards. If your are in UTC+N, then you will get the previous date.
moment(new Date('02-19-2014')).utc().format("YYYY-MM-DD HH:mm").toString()
moment(new Date('02-19-2014 12:00')).utc().format("YYYY-MM-DD HH:mm").toString()
(or)
You can try this:
moment.utc('07-18-2013', 'MM-DD-YYYY')
moment.utc('07-18-2013', 'MM-DD-YYYY').format('YYYY-MM-DD')
You do not need to call toString explicitly.

Categories

Resources