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>
Related
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.
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
In html file I need to display when that file was saved (time and date)
How can i do it? JavaScript?
<script>
var d = new Date(0);
document.getElementById("demo").innerHTML = d;
</script>
I need to know the the date when it has last been saved at the file system! And how can i change this 11-9-2014 at 23:7:55 to smth normal September 11 at 23:07?
document.lastModified will give you the time the document was last modified so far as the browser is able to determine it. You won't find anything closer unless you implement some method to embed the time stamp into the file when you save it.
Is this what you are looking for?
document.getElementById("demo").innerHTML = document.lastModified;
Demo here: http://jsfiddle.net/donal/w58v89Lh/
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.
When the page loads, I want a variable inside my JavaScript to hold today's date. So far I have this:
<script type="text/javascript">
var TodayDate = <%Eval('System.DateTime.Now') %>;
</script>
It massively bugs. Any suggestions?
Just use a javascript date object.
var today = new Date();
Or if you need the server time
var today = new Date('<%= DateTime.Now.ToString() %>');
is there anything in particular wrong with
<script type="text/javascript">
var TodayDate = new Date();
</script>
?
Why don't u use Javascript Date() object directly?
<script type="text/javascript">
var TodayDate = Date();
</script>
If you want to have a variable containing the date on the server, your way of doing is probably good (except for the missing apostrophes and the Date constructor). So it should be:
var today = new Date('<%= DateTime.Now.ToString() %>');
//this assumes that the server has such a locale that DateTime.ToString can be
//parsed by the javascript interpreter
But, if you need to have the date of the client:
var today = new Date();
You can use the previous people's answers of setting the date from within Javascript.
The problem you're seeing is that ASP is putting the string representing the current Date directly into the JavaScript source code. You need to put quotes around it so JavaScript sees a string and can parse it corretly.
Something like:
<script type="text/javascript">
var TodayDate = "<%Eval('System.DateTime.Now') %>";
</script>
which sets TodayDate to be a string. Are you going to use it as such or as a Date object?