Show the current server time from ASP.NET in four distinct time zones using JScript

I am in the process of customizing a SharePoint master page by adding a unique header that showcases the current time across four different time zones. However, I am faced with the challenge of determining the appropriate server-side time to use for accurate calculations, as relying on client-side time can lead to discrepancies due to varying time zones and incorrect system settings.

The desired display format for the time zones is as follows:

Dallas: (UTC-6 with DST support) Zulu: (UTC) Iraq: (UTC+3) Afghanistan: (UTC+4.5)

My main issue stems from my limited knowledge of ASP.NET, coming from a PHP background. I require a method to pass the serverTime variable to JavaScript for dynamic calculations to cater to each specific time zone.

UPDATE:

The goal is to have the clock update in real-time, reflecting every second change rather than displaying a static time.

UPDATE 2:

Provided below is the code snippet I am currently utilizing, any recommendations are appreciated:

<script runat="server">
  Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
  End Sub
  Public Function dtBase() As Double
  Dim d1 As Date = New Date(1970, 1, 1)
  Dim d2 As Date = Date.UtcNow
  Dim ts As TimeSpan = d2 - d1
  Return ts.TotalMilliseconds
  End Function
</script>

<html>
<head>

<script type="text/javascript">
// JavaScript functions for displaying and updating multiple time zones
// Code shortened for brevity...
</script>  
</head>
<body onload="zuluT();dallasT();iraqT();afgnT();">
<table>    
  <tr>
    <td>
      DALLAS: <span id="DALLAS">&nbsp;</span>
      ZULU: <span id="ZULU">&nbsp;</span>
      IRAQ: <span id="IRAQ">&nbsp;</span>
      AFGN: <span id="AFGN">&nbsp;</span>
    </td>
  </tr>
</table>

UPDATE 3:

Despite encountering issues with implementing the code due to restrictions set by the SharePoint server, further modifications were made to ensure its functionality. For future readers facing similar challenges, it's important to enable server-side scripting by modifying the web.config file. Detailed instructions are provided below:

<PageParserPath VirtualPath="/relative/path/to/your/file/here.aspx" CompilationMode="Always" AllowServerSideScript="true" />

Your feedback is greatly valued. Thank you.

Answer №1

The current time on the server is...

Now()

To display this server time in a web application, you can assign the value of the text property of a label control or the value property of a HiddenField control to Now() and then access it using Javascript in the OnLoad event. Another option is to create a WebService in .Net that sends back the server's current time in JSON or XML format, which can be fetched using Javascript.

Answer №2

It's not entirely clear why you would want to accomplish this using JavaScript. Or JScript for that matter. Please, whatever you do, avoid using JScript. JScript has caused many major companies to remain stuck with IE6 and Windows XP. (Take a look at the 4th comment here, along with other sources.)

JavaScript operates on the client side, so you're working with the time on the local machine of the client. It is feasible, yet more challenging, to determine the client's time zone and base your calculations off of that.

However, if you opt to utilize ASP.Net server-side code in C# or VB, you will automatically be utilizing the server's local time.

Local time equals UTC plus the time zone offset for that specific location, in addition to an extra offset (usually +1) for daylight saving time if applicable.

This resource provides information on how to retrieve UTC (or Zulu) time in C# or VB. Essentially, it involves:

DateTime.Now.ToUniversalTime();

To add hours to that:

DateTime.Now.AddHours(3);

You can input a negative number (-3) to deduct hours.

You can combine these actions to adjust UTC accordingly:

DateTime.Now.AddHours(3).ToUniversalTime();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Suggestions for Implementing a Smooth Login Experience in React Native

As a newcomer to the world of react-native, I am facing a challenge in creating a login feature that interacts with our backend APIs on AWS. The goal is to navigate to the home screen upon receiving a token from the API, while displaying an error message i ...

Using Mocha in Node to make XMLHttprequests

Currently, I am experimenting with using node-xmlhttprequest. Here is an example of what I have been working on: // f.js (function() XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest xhr = new XMLHttpRequest() xhr.open('GET ...

Perform table subtraction using linq

Here is my first post and I am in need of some help. I couldn't find a solution, so here I am: I have successfully created this SQL query that works as intended. string consultaSQL = @"SELECT a.GastosEstudio - ISNULL(SUM(b.Gastos ...

What steps are necessary to instruct multer to store the file in a specific directory?

Having some issues with multer while trying to save an image in a specific folder. The path for the file gets uploaded to mlab successfully, but the problem arises when attempting to retrieve the image from the designated folder as it fails to save there ...

Display information from a JSON file using Vue.js

Hello, I am currently attempting to display data from a JSON file using Vue.js. Below is my Vue file: <template> <div> <div class="card"> <div class="card-header"> <h4 class="card-title" ...

Tips for updating a webpage without causing it to fully reload in the browser?

Is there a method to automatically refresh a page in ASP.NET without the need for manual reloading? I am looking to have the page update every 5 minutes, even if it is not being accessed by any user. Appreciate your assistance ...

transfer to a different outlet when needing to circle back earlier

I'm currently working on implementing validation in a form, and one of the needed validations involves retrieving a value from an input field and checking its existence on the server or in a JSON file. However, there seems to be a problem where even ...

looping through an ajax function with addEventListener

Apologies in advance for any errors in my English. My task involves creating a simple webpage with just 2 links. When one of these links is clicked, it should load the content of a specific .html file on the same page. For example: clicking on link 1 (wi ...

Using an Ember color picker to dynamically change SCSS variables

Is there a way to allow an admin to change the skin of one of my websites by selecting a color from a palette that will update a universal SASS variable? I am aware of methods to dynamically change CSS using jQuery, but I specifically want to modify the S ...

Executing a completion function with node-cron: A comprehensive guide

My use of node-cron involves triggering a function on job execution (onTick) and another function when the job ends (onComplete). However, I'm facing unexpected behavior. If the job does run, then onComplete never executes: var CronJob = require(&ap ...

Only one instance of the Next.js inline script is loaded

Incorporating Tiny Slider into my Next.js application has been a success, but I am facing an issue with the inline script that controls the slider's behavior. The script loads correctly when I first launch index.js. However, if I navigate to another p ...

Enhancing user experience with dynamically resized Jumbotron images based on screen sizes in HTML

I am experiencing an issue with the jumbotron images on my website where they become increasingly zoomed in as the screen size decreases, and are extremely zoomed in on mobile devices. Is this a normal behavior of jumbotrons? I do understand that the image ...

Choosing elements from an array in real-time

Having a javascript array with JSON objects as values, such as: var array = [{"div0" : [2, 1, 3, 5]}, {"div1" : [1,5,7,8]}, {"div2" : [6,9,11]}]; I am able to manually select each object like array[0].div0, array[1].div1, array[2].div2. However, I' ...

Leverage the exported data from Highcharts Editor to create a fresh React chart

I am currently working on implementing the following workflow Create a chart using the Highcharts Editor tool Export the JSON object from the Editor that represents the chart Utilize the exported JSON to render a new chart After creating a chart through ...

Building protected "exclusive website sections" utilizing near-serverless website structure (AJAX template wizardry)

At the outset, I apologize if my English is not up to par; I will do my best to be as clear as possible. I am in the process of planning a web site with the following architecture: The site will consist of static pages served to clients using JavaScript t ...

Verify if the program is operating on a server or locally

My current project involves a website with a game client and a server that communicate via sockets. The issue I'm facing is how to set the socket url depending on whether the code is running on the server or my local PC. During testing and debugging, ...

Is it advisable to incorporate a failsafe in my ajax requests, or is it deemed unnecessary?

From what I understand, javascript operates in a single-threaded manner, meaning only one task can be performed at a time. Currently, I am developing a fully ajax-driven webpage where all navigation is accomplished by sending data to the server and display ...

Using React's useEffect and useContext can cause issues with certain components, particularly when dealing with dynamic routes

Currently, I am developing a React blog application where posts are stored in markdown files along with metadata in Firestore. The content .md files are saved in Cloud Storage. In the App component, I utilize useEffect to retrieve the metadata for each pos ...

Using Regex and Javascript to extract the base URL from a string

I am attempting to extract the base URL from a string without using window.location. It must eliminate the trailing slash It must be done using regex instead of New URL It should handle query parameters and anchor links In essence, all of the following ...

Generating dynamic div elements using jQuery

I am currently working on developing a button that will automatically generate pre-formatted divs each time it is clicked. The divs consist of forms with fields that should already be populated with data stored in JavaScript variables. Example: <d ...