Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
Convert Epoch timestamp to ISO in javascript

I have a function in Java to convert an Epoch date to ISO 8601, but I've come across the need to do it in Javascript. I have it somewhat working in JS but it needs to be localized to the timezone.

Java version:

public static String epochToIso8601(long time, String Zone) {
    String format = "yyyy-MM-dd'T'HH:mm:ssX";
    TimeZone timeZone = TimeZone.getTimeZone(Zone);
    SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
    sdf.setTimeZone(timeZone);
    return sdf.format(new Date(time));
}

Param1: -157737600000

Param2: PST

Output: 1965-01-01T00:00:00-08

My attempt in Javascript:

      function epcov(epoch, timezone)
      {
        var someValueNum = Number(epoch);
        var s = new Date(someValueNum);
        return s.toISOString();
      }

Essentially I want the same thing that's coming out of Java, but it's outputting: 1965-01-01T08:00:00.000Z

By the way, I'm already splitting the date and time up from something that looks like this, so if there is a better to just pass in the following as one string and let Javascript parse it, that would be amazing:

/Date(-157737600000-0800)/
almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

We can convert the string /Date(-157737600000-0800)/ to a unix time (in ms), and a UTC offset in HHMM using String.match().

The HHMM UTC offset can then be converted to a UTC offset in milliseconds by multiplying the HH value by 3600000 and the MM value by 60000.

We then create a new Date object using the unix time and offset values, since we know the exact offset at that point in time.

We then format using Date.toISOString() and replace the 'Z' UTC offset timezone value with the actual UTC offset string (e.g. '-08:00').

function parseAndFormatDate(date) {
    const [,millis, offset] = date.match(/([+\-]+\d+)([+\-]+\d+)/);
    const offsetMillis = (offset.slice(0, 1) + '1') * (offset.slice(1, 3) * 3600000 + offset.slice(-2) * 60000);
    const formattedOffset = offset.slice(0, 3) + ':' + offset.slice(-2);
    return new Date(+millis + offsetMillis).toISOString().replace('Z', formattedOffset);
}

console.log(parseAndFormatDate('/Date(-157737600000-0800)/'));
console.log(parseAndFormatDate('/Date(+1664271413000+0100)/'));
.as-console-wrapper { max-height: 100% !important; }

almost 4 years ago · Santiago Trujillo Relatório

0

I hope this helps you in some way.

JS doesn't have good (any?!) support for exporting an ISO 8601 string in a specified time zone. So you have to construct the string yourself, manually.

The use of the Swedish locale is to get an ISO 8601-like basis from which to pull the elements of the date time. Unfortunately there is no ISO 8601 formatting locale.

Note that the following will only reliably work with IANA timezone names because timezone abbreviations (PST, CST, BST) can be ambiguous. For example: V8 will accept 'PST' but SpiderMonkey will not.

// Get wall clock date at the specified tz in IS0 8601 
const getISO8601Date = (d, timeZone) =>
    d.toLocaleString('sv-SE', { timeZone, dateStyle: 'short'})

// Get wall clock time at the specified tz in IS0 8601 
const getISO8601Time = (d, timeZone) =>
    d.toLocaleString('sv-SE', { timeZone, timeStyle: 'medium'})

// Get time zone offset in specified tz in IS0 8601 
const getISO8601TimeZoneOffset = (d, timeZone) => {
    const s = d.toLocaleString('sv-SE', { timeZone, timeZoneName: 'longOffset' })
    const result = /GMT(?<offset>[+−]\d\d:\d\d)/.exec(s) // Use regexp to pull out offset
    return result ? result.groups.offset : 'Z'
}

// Put together an ISO 8601 string representing the wall clock at the specified date, in the specified timezone
const getISO8601Dtg = (instant, timeZone) => {
    const d = new Date(instant)
    return `${getISO8601Date(d, timeZone)}T${getISO8601Time(d, timeZone)}${getISO8601TimeZoneOffset(d, timeZone)}`
}

// Only reliably accepts IANA timezone names eg. 'America/Los_Angeles'
console.log(getISO8601Dtg(-157737600000, 'America/Los_Angeles')) // 1965-01-01T00:00:00−08:00

Relevant.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda