How to I get the date and time from SharePoint Server 2007 and pass this value as a Javascript variable?

1 Answer 1


No need to get it from SharePoint; use a content editor web part (CEWP) to get the date directly in JavaScript. Once you've dropped a CEWP on the page, click to add content, choose HTML > Edit HTML Source from the ribbon. Now put a JavaScript block in it: <script type="text/javascript">....</script>. For the block, use this example to display the date: http://www.mediacollege.com/internet/javascript/date-time/

Something as simple as var d = new Date(); will fetch the current date as an object, but you'll have to use the example to format it the way you need.

If you need to fetch the server time, you can a) use the Client Object Model to connect to the server via JavaScript; b) use some simple server-side code to fetch the system date on your master page or page layout and store it in a JavaScript variable:

var systemDate = new Date("<%= System.DateTime.Now %>");

Now you're using JavaScript client side and fetching the date from the system server side. You have it stored in a Date object in JavaScript so you can use the earlier example to manipulate it as needed. Ensure that server-side code blocks are enabled in your web.config for this to work.

  • Thanks Alex, the reason why I asked the question is that from my understanding, JavaScript is a client language (do correct me if I am misunderstand) and so JavaScript report the date and time could be from the client machine rather from the server machine (do correct me if I misunderstand) Oct 28, 2011 at 0:56
  • Please see my updated answer.
    – Alex C
    Oct 29, 2011 at 19:05
  • One thing I just noticed: I don't think the Client Object Model is available on SP2007. So best bet is to use Option B in code above. Let me know how it goes.
    – Alex C
    Oct 29, 2011 at 19:32
  • Thanks Alex so much. I wish to use your Option B but our Server Administrator is so "paranoid" that he refused to enabled the server-side code blocks in the web.config. Is there other way to retrieve the server date and time? Oct 31, 2011 at 7:50
  • Some options: If you need a date/time display mechanism, use a free web part, such as this: amrein.com/apps/page.asp?Q=5729. Bamboo also has a free date/time web part. Another option: Create a list with a DateTime column; then set the date to today -- sridharu.blogspot.com/2009/01/…. In addition, you can add a calculated column to format the date anyway you need via formulas (abstractspaces.wordpress.com/2009/05/02/…). Then drop the web part on your page.
    – Alex C
    Oct 31, 2011 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.