Skip to main content
dublove
Legend
August 5, 2025
Answered

How to read Internet time with JS?

  • August 5, 2025
  • 3 replies
  • 1174 views

Hi everyone.

I want to check the system time when running the script every Monday.

Thank you.

Correct answer m1b

I just want to read the Internet date,
then compare it with the local date. Only when the two are the same will the script be able to run.

 


Do you think this will suit you? It grabs the timestamp from a HTTP request. Not an Internet time server, but might be a reasonable proxy.

 

(function () {

    // Usage:
    try {

        var nowAccordingToGoogle = getInternetTimeViaHTTP();
        var nowAccordingToComputer = new Date();

        alert('Times:'
            + '\nUTC time from google.com is:\n' + nowAccordingToGoogle.toUTCString()
            + '\nUTC time from your computer is:\n' + nowAccordingToComputer.toUTCString()
        );

    } catch (e) {
        alert("Error: " + e);
    }

})();


/**
 * Returns the time according to response of HTTP request to google.com.
 * @7111211 ChatGPT and m1b
 * @version 2025-08-06
 * @Returns {Date}
 */
function getInternetTimeViaHTTP() {

    var socket = new Socket();

    if (!socket.open("google.com:80", "UTF-8"))
        throw new Error("Could not open socket");

    // Send a minimal HEAD request
    socket.write("HEAD / HTTP/1.0\r\nHost: google.com\r\n\r\n");

    // Read entire response
    var resp = "";

    while (!socket.eof)
        resp += socket.read(1024);

    socket.close();

    // Find the Date: header
    var m = resp.match(/^Date:\s*(.+)$/mi);
    if (!m)
        throw new Error("No Date header in response");

    // Parse (e.g. "Tue, 06 Aug 2024 12:34:56 GMT")
    var dateStr = m[1];

    return new Date(dateStr);

};

 

3 replies

rob day
Community Expert
Community Expert
August 5, 2025

The ExtendScript date object and methods are here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Date.html#d1e2097__d1e2487

 

This checks if the day is Monday

var d  = new Date().getDay();  
if (d == 1) {
	alert("Today is Monday")
} else {
    alert("Today is not Monday")
}
dublove
dubloveAuthor
Legend
August 5, 2025

Hi rob day.

Thank you very much.

 

I gave up.
I can find some information about the local date.
But I don't know how to get the time from the network, such as the system default “time.widows.com” time.

rob day
Community Expert
Community Expert
August 5, 2025

Are you looking for an accurate time? This gets the current Greenwich Mean Time (GMT) from that you could get the current time for any time zone.

 

var d  = new Date()  
var h = d.getUTCHours()
var m = d.getUTCMinutes()
alert("Current GMT: " + h + ":" + m)
m1b
Community Expert
Community Expert
August 5, 2025

Hi @dublove what do you mean by "Internet date"? Do you mean ISO-8601 date format? If so, here is a function I use to get it from a date object. If you pass no date it will give you today's date.

- Mark

 

/**
 * Generate ISO-8601 short date string: YYYY-MM-DD.
 * @author m1b
 * @version 2021-06-15
 * @param {Date} [date] - the date (default: today).
 * @param {String} [delimiter] - string to delimit values (default: '-').
 * @returns {String}
 */
function getISODateString(date, delimiter) {

    // if no Date supplied, use today
    date = date || new Date();
    delimiter = delimiter || '-';
    
    return [
        date.getFullYear(),
        ('0' + (date.getMonth() + 1)).slice(-2),
        ('0' + date.getDate()).slice(-2)
    ].join(delimiter);

};
dublove
dubloveAuthor
Legend
August 5, 2025

Hi m1b.

Thank you very much.

I want to synchronize to time.windows.com
Synchronize to day is sufficient; just try once on Monday.

Only synchronize to the day, so you shouldn't need to worry about the time difference.

Like this:

var myDate = new Date();
var myYear = myDate.getFullYear();

//var myMonth = ("0" + (myDate.getMonth() + 1)).slice(-2); // Add leading zero
var myDay = ("0" + myDate.getDate()).slice(-2); // Add leading zero
var myDay = myDate.getDay();

//If today is Monday, synchronize the server time.
if (myDay == 1) {
    SynServerTime()
}

 

m1b
Community Expert
Community Expert
August 5, 2025

Doesn't Windows already synch to a time server? What does it have to do with ExtendScript? If you really need to do this from an extendscript you will need to access an external process, eg. through VB script. I have no idea how to do it on Windows (I use MacOS). Good luck.

brian_p_dts
Community Expert
Community Expert
August 5, 2025

Look into the Date object

 

dublove
dubloveAuthor
Legend
August 5, 2025

Hi,brian_p_dts

There doesn't seem to be any Internet time there.

This?

d();
function d() {
    var d = new Date();
    document.getElementById("day").innerHTML = + d.getDate() + 'day';
}
alert(d);