• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

problem in date

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
jyothsna kumari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response.
Can you please help me further by giving some code.
Jyothsna
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic