0

I am new to web application making a online examination website and have a script which is currently taking static date and time. I want it to the system current date and time plus(add current time with) a value from jsp variable(i.e the time interval of the examination) Currently my javascript is as follows:-

<script language="JavaScript">
TargetDate = "12/26/2013 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>

Add i have a jsp variable "t" to be added to current time as to make the target time=current time + "t".

Note- This is for countdown timer in my webpage.

Thankyou.

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
sam
  • 43
  • 2
  • 9
  • Please refer [this](http://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript) too. And for adding javascript and jsp variable, you may have to use a hidden variable to write jsp value to it, and use javascript to read it and add what you are saying. – Siva Tumma Dec 25 '13 at 16:34
  • Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. – Andrew Thompson Dec 25 '13 at 16:38
  • @sivatumma I have that variable "t" in my jsp but how to add it to the script?? can i just use <%t%>. – sam Dec 25 '13 at 16:44
  • Though I am not having tomcat installed, I try to tell you theoretically. You will put your jsp variable in a hidden field in your html like this : ``. Now in your javascript, you will take this using `var myJavascriptVar = 'some value'; alert(document.getElementById('myJspVar').value + myJavascriptVar);`. Please note this is how you could do, but you have to fiddle as I do not have tomcat to test for correctness. – Siva Tumma Dec 25 '13 at 16:50
  • @sivatumma i am done with the system date stuff thanks a lot but i am unable to get how to add my jsp variable to my current minutes – sam Dec 25 '13 at 17:19
  • now it would be better to show your code. copy a short snippet where you are trying to add them ... – Siva Tumma Dec 25 '13 at 17:31
  • `` – sam Dec 25 '13 at 17:43

2 Answers2

1

You can use scrptlets or expression .

<script language="JavaScript">
     TargetDate = "<%=new java.util.Date()%>";
     . . . 
</script>

For learning more , refer link

For Date Formatting (Till Java7) :

You can use Simple DateFormat format codes .

   <%
      Date dNow = new Date( );
      SimpleDateFormat ft =new SimpleDateFormat ("MM/dd/yyyy  hh:mm a");

   %>

   <script language="JavaScript">
         TargetDate = "<%=ft.format(dNow)%>";
         . . . 
    </script>
Community
  • 1
  • 1
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
0

Poor Answer

how to get this in the format "12/26/2013 5:00 AM"?

To directly answer your question… To do so on the Java side on the server (as opposed to client-side in JavaScript), use the bundled classes java.text.DateFormat and java.text.SimpleDateFormat.

Better Answer: Joda-Time

A better answer is to tell you to avoid java.util.Date and java.util.Calendar classes. They are notoriously badly designed and implemented. Instead use a good date-time library. In Java that means either Joda-Time (open-source third-party library) or the new java.time.* classes defined by JSR 310 and bundled with Java 8 and meant to supplant the old j.u.Date/Calendar.

If you literally want only the format like "12/26/2013 5:00 AM", you can define a pattern in Joda-Time. You'll find many examples of this in other questions here on StackOverflow.com.

If you know the user's Locale and time zone name you can use those to format the date as a familiar string presentation.

Example code…

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime now = DateTime.now();

// Style is defined by a pair of letters, one for date portion, the other for time portion.
// Letters are the first letter from: Short/Medium/Long/Full
// This example might be appropriate for a French person in Puducherry India (formerly Pondicherry, ex-colony of France).
DateTimeFormatter formatter = DateTimeFormat.forStyle( "SS" ).withLocale( Locale.FRENCH ).withZone( DateTimeZone.forID( "Asia/Kolkata" ) );
String dateTimeString = formatter.print( now );
System.out.println( "dateTimeString: " + dateTimeString + " for: " + now );

// By comparison.
DateTimeFormatter formatter_FF = DateTimeFormat.forStyle( "FF" ).withLocale( Locale.FRENCH ).withZone( DateTimeZone.forID( "Asia/Kolkata" ) );
String dateTimeString_FF = formatter_FF.print( now );
System.out.println( "dateTimeString_FF: " + dateTimeString_FF + " for: " + now );

When run (with my default time zone being US west coast)…

dateTimeString: 26/12/13 02:35 for: 2013-12-25T13:05:09.282-08:00
dateTimeString_FF: jeudi 26 décembre 2013 02 h 44 IST for: 2013-12-25T13:14:41.841-08:00

Experiment

Experiment if you wish. For United States, try local of Locale.US and time zone of America/New_York or America/Indiana/Knox.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154