How do you compare the client date and server date? - javascript

I have a code that runs to select a date from calender, but I want to set the current_date to that of the server in the mentioned calender. And with a functionality that compares client_date with server_date.

That's possible. As suggested elsewhere, you should convert the dates to strings for two reasons:
Easier to read/understand when you search for a bug
The "current time" is always different between client and server
To send the current date to the client, you can use a hidden form field or an AJAX request or a web service or REST or JSON or JSONP or ... well, there are too many methods to do it.
To compare dates, convert the string to a Date object and then use the usual methods to compare them (compareTo() in Java and <,==,> in JavaScript).

You can do it by using the following way:
In Java
public class YourJavaClass{
public static String getServerDate()
{
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
}
}
In jsp
$('input.datepicker').datepicker(
{
minDate : new Date(<%out.print(YourJavaClass.getServerDate());%>)
});

The easiest way is to compare both dates with conversation to milliseconds. Both languages provides methods for it.
Javascript:
<script type="">
var myDate = new Date();
var milliseconds = myDate.getTime();
</script>
Java (JSP):
<script type="text/javascript">
<%!
Date myJavaDate = new Date();
long myJavaMilliseconds = 0L;
myJavaMilliseconds = myJavaDate.getTime();
%>
var myJavaMillisondsInJs = <%= myJavaMilliseconds %>;
</script>
/* Put code here to compare between those dates */
If you want to compare the dateas server side you have to submit the javatime to the server via Ajax or a form.

One way of doing it is to pass the current date from your server script to the template, so when the pages renders, javascript can access it and make comparison. This has obvious issue - if the user will leave the page open till next day, the date will be out of date.
Other way is to use asynchronous call to your back-end and obtain the date from there.

Related

APPS SCRIPT DATE FORMAT ISSUE

I'm trying to send a table from google sheets to gmail using apps script, i have multiple dates and i want to send in this format "dd-mm-yy", but the problem is that i need to use utilites.formatdate which coverts the cell into string, so when i try to send the email it diplays that the parameters dont match the method, i think this is because in some cells date could be not fullfilled, so theres nothing written in the cell, because of this error i cant send the email, if i write dates in the cells it works but there are cases when they are not fullfilled as i said, how can i solved this problem and send the email no matter if all the dates are filled or not?
var fecha = registro.getRange("c16").getValue();
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
This is the way i tried to change the format, and if there is someting written works but
var fecha2 = registro.getRange("e16").getValue();
var fecha2F = Utilities.formatDate(fecha2, "GMT","dd-MM-YY");
In the second there is nothing so it displays the error
Hope you can help me guys
I'm learning so all kind of advices you give to me are welcome
Try this:
var fecha = new Date(registro.getRange("c16").getValue());
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
Try to initiate Date by adding new DATE() to use the converted string as DATE type and after that you can format it as you want
so your code will be
var fecha = new DATE(registro.getRange("c16").getValue());
EDIT
var fechaF = Utilities.formatDate(fecha, "GMT","dd-mm-yyyy").setNumberFormat('dd-mm-yyy');

getElementByName() for date

I am trying to evaluate inputs on client side using javascript. The getElementByName() method is working for text type inputs. But I want to compare two dates using JS. But getElementByName() is not working for date type inputs!
Someone please help!
Here is my code:
<script type="text/javascript">
function validate()
{
var startdate = document.getElementByName("startdate").valueOf();
alert(startdate);
var enddate = document.getElementByName("enddate").value;
submitOK=true;
if(startdate>enddate)
{
alert("start date should not be greater than end date");
submitOK=false;
}
return submitOK;
}
</script>
If you want to get started quickly, add unique ids to both your date inputs, and then use
document.getElementById("startdate").value;
for example to get both values.
And then regarding of their format, a string comparison can be enough (I don't know because you did not mention anything about that), or then you could do something like
var endDate = document.getElementById("startdate").value;
endDate = new Date(endDate);
That will give you access to date specific methods that can be handy.
More on this here.

Full Calendar Get current date

I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
Here is the relevant section of the config file,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
So as you can see it is displaying the current date in view. My question is how do I access the _d bit of the Moment variable so I can use it?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
To get the current date of the calendar, do:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment object. You can then format it as a string in the usual date format like so:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
FullCalendar's getDate returns a moment object, so you need moment's toDate() method to get date out of it.
So, in you code try:
console.log(currentDate.toDate());
and that should return a date object.
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date

Deserializing JSON dates timezone-less

I have a problem that is breaking my head since yesterday and don't know how to deal with it.
I have a date field in my database that contains the following value:
Then my application get the value and send that to my web form. (the value still the same :) thats fine!!
In client side I put a break with a javascript alert to see the value that is comming from JSON (the value still the same :) thats fine!!
The code in client side for the alert is this:
// transaction json model
var jsonTransaction = #(Html.Raw(Json.Encode(this.Model.Transaction)));
alert(new Date(parseInt(jsonTransaction.Date.substr(6))));
Now when I send back the value to the server this is what I get
And finally after deserialization of the JSON my date time is wrong!! instead of Day 7 its now Day 8???????
This is the code for deserializing:
public JsonResult SaveBill(string jsonTransaction, string jsonTranDetails)
{
_appState = this.AppState;
JsonResult returnVal = returnVal = Json(
new { Success = true }
);
var transaction = JsonConvert.DeserializeObject<BillTransaction>(jsonTransaction, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
Any clue on how to solve this issue with dates, I should get the same date because I didn't change anything. Hope someone can guide me for a solution.
Thanks in advance.
Java script use universal time when it parse the date as currentdate = new Date(123232)
so when you send date to client convert it to ISO date such as
make sure the date is in UTC before convering it to strong .
return String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", dt);
bty I already created JSON Converted to override any date serialization to client side to use the above function .

How to get Client machine time on server on pageload event

I want to use client system date on page load event,
How can I do that.
Since this is not a post back so please let me know how to do it.
Thanks
var a = "&date=" + (new Date().replace(/\s+/g, ""));
Just append the value of a to all your links back to the server. On the serverside you will see the datetime come across in the HTTP request.
var currentDate = new Date()
The currentDate object will have the methods:
getMonth()
getDate()
getFullYear()
This should give you what you need.
Edit:
Since you mentioned time in the title of the post, it's probably worth mentioning that currentDate will also have:
getHours()
getMinutes()
should you want to display the current time as well.
Edit 2:
To tie into page loading
<script type="text/javascript">
function buildDate() {
var date = new Date();
/*build date string here*/
}
</script>
<body onload="buildDate()">
Other stuff here...
</body>

Categories

Resources