Convert seconds since epoch to local time string

Convert seconds since epoch to local time string

25
Basic UserBasic User
25

    Mar 19, 2013#1

    The programmers have my request for a tool button (similar to "Encode/Decode Base64") that would take an epoch value and convert it to a time string at UTC. But I don't see it in the recent UE19.

    I found this timestamp conversation, and it is very interesting and informative, but doesn't fit my needs.

    I am searching for a macro (assigned to a button) that will take a highlighted number - the epoch number - and return and replace the selected text with a date/time string (maybe recalculated with timezone offset).

    (Can a user write a script that will add a setting to the Configuration dialog? In there would be the way to store the format string and time zone offset.)

    Thank you.

    6,602547
    Grand MasterGrand Master
    6,602547

      Mar 19, 2013#2

      A macro can't do that, just an UltraEdit script.

      It is not possible to add a script to toolbar as symbol. But you can add the script with Scripting - Script List to the list of scripts and assign a hotkey or chord for fast execution. Or the script is executed by a click on the script in menu Scripting or by a double click on the script in the Script List opened via View - Views/Lists - Script List if currently closed completely.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Is a string selected in active file?
         if (UltraEdit.activeDocument.isSel())
         {
            var sSecondsSinceEpoch = UltraEdit.activeDocument.selection;
            var nSecondsSinceEpoch = parseInt(sSecondsSinceEpoch,10);
            // Is the selected string really a number?
            if (nSecondsSinceEpoch != NaN)
            {
               // Create a date object with milliseconds value since epoch.
               oDateTime = new Date(nSecondsSinceEpoch*1000);
               // Get full year (with century) in local time as string.
               var sYear = oDateTime.getFullYear().toString(10);
               // Get month with leading zero in local time as string.
               var nMonth = oDateTime.getMonth() + 1;
               var sMonth = (nMonth < 10) ? "0" : "";
               sMonth += nMonth.toString(10);
               // Get day with leading zero in local time as string.
               var nDay = oDateTime.getDate();
               var sDay = (nDay < 10) ? "0" : "";
               sDay += nDay.toString(10);
               // Get hour with leading zero in local time as string.
               var nHour = oDateTime.getHours();
               var sHour = (nHour < 10) ? "0" : "";
               sHour += nHour.toString(10);
               // Get minute with leading zero in local time as string.
               var nMinute = oDateTime.getMinutes();
               var sMinute = (nMinute < 10) ? "0" : "";
               sMinute += nMinute.toString(10);
               // Get second with leading zero in local time as string.
               var nSecond = oDateTime.getSeconds();
               var sSecond = (nSecond < 10) ? "0" : "";
               sSecond += nSecond.toString(10);
               // Build complete time string in format dd.mm.yyyy hh:mm:ss.
               var sDateTime = sDay + "." + sMonth + "." + sYear + " " +
                               sHour + ":" + sMinute + ":" + sSecond;
               // Overwrite the selected text in file with this time string.
               UltraEdit.activeDocument.write(sDateTime);
            }
         }
      }
      Please read the comments in this script and adapt the output format to your requirements.

      See also the documentation for Date object of JavaScript core. As you can read there, there are also UTC time functions.

      To use a different time zone simply add/subtract right offset in milliseconds on line:

      Code: Select all

      oDateTime = new Date(nSecondsSinceEpoch*1000);
      See also similar topic Convert epoch time stamps to UTC/local time.