File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem in  date" Watch "problem in  date" New topic

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',
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;

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

this is working 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.
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.
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,
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
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?
[ March 03, 2004: Message edited by: Eric Pascarello ]
Pauline McNamara

Joined: Jan 19, 2001
Posts: 4012
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
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?
I agree. Here's the link:
subject: problem in date
It's not a secret anymore!