aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes problem in  date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem in  date" Watch "problem in  date" New topic
Author

problem in date

jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
hi all,
i am generating the current date on my jsp page by doing something like this.
<script language="javascript">
function start()
{
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}
var months = new makeArray('January','February','March',
'April','May','June','July','August','September',
'October','November','December');
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
fulldate=months[month]+" "+day+ "," + " " + year;
return fulldate;

}
</script>
and i am getting the date on the page by calling the function like this
<body>
<script language=javascript>
var date=start();
document.write(date);

</script>
</body>
this is working fine.now the problem is,the date which i am getting is my system date.if i change the date of my system,then that changed date is comming.i want the actual date to be displayed irrespective of the system date because the date plays a crucial role in my application.
pls can anybody help me to solve this problem.
Jyothsna
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
var date = new Date();
JavaScript creates an object of date according to the time on the local machine.So if client changes system time,javascript date object will also changes.
Pass actual date from JSP to javascript and assign it to date constuctor.
You will get actual date.You may use toLocaleString() method to display date into user's system date convention.There are other mothods as well to modify date format.


If you want to become a rich, do not work for others but make others to work for you.
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
Thanks for your response.
Can you please help me further by giving some code.
Jyothsna
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
For ex. if your JSP variable contains date string like,
strCurrDate = Mar 07, 2004 ; (This date could be server date or get date
from anyother source) //This would be your actual date
then in javascript simply set it into date constructor
var date = new Date("<%=strCurrDate %>");
Rest is same procedure.Just need to change Date() constructor.
Here after, not matter what client system date is, javascript always
display Mar 07, 2004.If you need different date format ,just explore with giving different date format in Date() constructor.
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
let me put the question in the other way.Say i have server and client both running on the same system and the date of that system is wrong.In that case my server also takes the system date which is wrong.So how to get the actual date.
Thanks in advance,
Jyothsna
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You need to do it on the server side, it was said before that that is the only way to do it safely. Why would the date be wrong on the server if that is the case then I think you should fix the time? It may be in another time zone, and you might need to take in account for that?
The only true way to do it is if you are standing there and making sure that the date is right. What says that your watch is also set wrong?
Eric
[ March 03, 2004: Message edited by: Eric Pascarello ]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
It's a bit hard to tell what would be "wrong" about the system time - maybe you're trying to avoid working with the local time? Maybe using Universal Coordinated Time (UTC) would help?
You can get that from your date object with a bunch of methods like getUTCMonth etc. They're listed at
http://www.w3schools.com/js/js_datetime.asp
where you can play with them directly, that's pretty fun.

You wouldn't necessarily have to go through the date and pick out the individual parts - there's a toGMTString() method that gives you the Greenwich Mean Time. (For most practical purposes that's the same as UTC.)

and that will get you the Greenwich Mean Time in this form:
Wed, 03 Mar 2004 14:57:59 GMT
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you want to ensure that your server time is accurate, you should investigate setting up an NTP server (Network Time Protocol). This service would sync with 'known accurate' NTP servers elsewhere. Much like Certification authorities... at the very top are universities and US Navy atomic clocks. Eventually it gets down to a 2nd or 3rd tier provider that can keep your NTP server at 'the correct time'. Then use this server to keep all your other servers on your network on 'the correct time'.

Is that what you're wanting?

http://www.eecis.udel.edu/~mills/ntp/servers.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in date