Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
sir,


how to get code for server date time in java script

i am using client system, through access server time.
Posted
Updated 6-Dec-11 19:48pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-11 1:55am    
Do you have an access to server-side code? Why would you need server time, not UTC, which would be logical? It you thing about it: who cares where the server is?
--SA

Since hidden fields are available in any web application so you can embed a hidden field in your page and set its value in server side and use it in client side javascript. so here is a sample ASP.NET solution :

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testdate.aspx.cs" Inherits="testdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body onload="writeServerdDT()">
<script>
    function writeServerdDT() {
        document.write(document.getElementById('serverDateTime').value);
    }
</script>

    <form id="form1" runat="server" >
    <asp:HiddenField  ID="serverDateTime" runat="server"/>
    <div>

    </div>
    </form>
</body>
</html>




and the CodeBehind :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class testdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        serverDateTime.Value = DateTime.Today.ToString();
    }
}



Hope it helps.
 
Share this answer
 
Well... this might not be totally reliable, but you could use AJAX to request something from the server, then get the server time from the response header:
http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900