I tried create markers by JSON parse from C #.I have a small problem about datetime compare in javascript.
var nowDate= new Date();
var LastTenMin= new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(),nowDate.getHours(),nowDate.getMinutes()- 10);
var Time1= data2.LastRecordTime;
var image2;
var status;
if (new Date(Time1) < new Date(LastTenMin)) {
image2 = '/Images/truckOnline.png';
status = "Truck is online."+"\n"+"Last seen:"+" "+Time1,
}
else {
image2 = '/Images/truckOffline.png';
status = "Truck is offline"+"\n"+"Last seen:"+" "+Time1,
}
else is not working ! There are truckOnline markers on google map.Where is my mistake ?
And LastRecordTime format like this in SQL : 04.12.2013 01:03:00
LastRecordTime=CONVERT(VARCHAR(10), [ReadTimeColumn], 104) + ' ' + CONVERT(VARCHAR(8), [ReadTimeColumn],108)
Mehmet,
Looks like you made a typo:
Should be (note the comma):
Also you were trying to create a new date object from a date object, this is incorrect:
And here is a more complete solution:
Here is the solution without comments:
My whole solution is assuming that the string contained in data2.LastRecordTime is in the format: "MM.DD.YYYY HH:MM:SS".
I solved by SQL.
I set a new colum for difference minute between now and ReadTime.
To compare dates with time in
Javascript
we need to pass them inDate
object as "yyyy/mm/dd HH:mm" format for e.g. like"2014/05/19 23:20"
. Then you can just compare them with > or < less then symbol according to your business rule. Please see given below code for more understanding.You can also see a working demo here
This is going to sound like a cop out, but I would switch to MomentJS so you get the following code:
Remember, JavaScript has random off-by-one issues for the date and month (one is zero-based, the other is one-based). The problem most likely is in this line:
If you switch to MomentJS, these kind of problems will disappear. I have lost many hours fighting these same issues, so I understand!
P.S. Try out the calendar() formatting feature... it may be a good fit in your UI.