Home > ASP.Net, Javascript > Use WebService with Javascript to Get Auto DateTime

Use WebService with Javascript to Get Auto DateTime

Hi all,

try this example to Use WebService with Javascript to Get Auto DateTime

1) open vs2008 and create a new web site and add new WebService name it “WebService.asmx” and add this code :-

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components

 public string GetTime()
 return DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");


2) Add new Web Page and in ASPX Design add this :-

<head runat="server">
 <title>Untitled Page</title>
 <script type="text/javascript" language="javascript">
 var interval = 0;
 function pageLoad() {
 interval = window.setInterval("callWebService()", 3000);

function callWebService() {
 WebService.GetTime(successCallback, failCallback);

function successCallback(result) {
 var divOutput = document.getElementById("divOutput");
 divOutput.innerHTML = result;

function failCallback(error) {
 alert('Error: ' + error.get_exceptionType());

 <form id="form1" runat="server">
 <asp:ScriptManager runat="server" ID="scriptManager1">
 <asp:ServiceReference Path="~/WebService.asmx" />
 <div id="divOutput">

then run your page you will find time change every 3 sec this is because i call the result from webservice

Hope this helps

Good Luck

Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: