Doubt Javascript Date() [front-end]

Asked

Viewed 149 times

0

I’m building a personal blog using Github Pages and a desktop app con NWJS to do the back-end administration however I have a big question when presenting dates on the front-end.

As it comes to a "static" website the posts are files markdown and its file configuration parameters json, in the desktop application use Moment.js to save the publication (or editing) timestamp but do not know what the best approach (or method) to deal with the user’s Timezone once the function javascript responsible for showing how long the publication (edition) was made depends on the user’s Timezone.

When making a post the timestamp is based on the time of my computer (or custom setting Moment.js) bad users of a different Timezone received different times on the front end based on their own times.

I’m not sure how to proceed to resolve this? Thank you for any help or referral links.

Function.js

/**
 * Time ago (UNIX)
 * @param {string|number} time
 * @return {string} human readable time elapsed
 */
function timeAgo(time){
    var round = Math.round,
        now = new Date,
        t;
    //
    let format = function(n, unit){
        let a = 'hour' == unit ? 'an' : '1';
        unit = 1 == n ? unit : (unit !== 'month' ? unit + 's' : unit);
        return (1 == n ? a : n) + ' ' + unit;
    };
    // past / future
    let diff = (time > now) ? (time - now) : (now - time);
    // just now
    if (1e3 > diff) return 'now';
    // s, m, h, d, w, m, y
    if (60 > (t = round(diff / 1e3))) return format(t, 'second');
    if (60 > (t = round(diff / 6e4))) return format(t, 'minute');
    if (24 > (t = round(diff / 3.6e+6))) return format(t, 'hour');
    if (7 > (t = round(diff / 8.64e+7))) return format(t, 'day');
    if (4.34812 > (t = diff / 6.048e+8)) return format(round(t), 'week');
    if (12 > (t = round(diff / 2.63e+9))) return format(t, 'month');
    if (10 > (t = round(diff / 3.156e+10))) return format(t, 'year');
    // decades
    return format(round(diff / 3.156e+11), 'decade');
};

let btn = document.getElementById('click');
btn.addEventListener('click', function(ev){
    ev.preventDefault();
    let now = timeAgo(1502085380495);
    let span = document.getElementById('show');
    span.innerHTML = 'Published there ' + now;
}, false);
<button id="click">click to example</button>
<br>
<br>
<span id="show"></span>

  • Take a look at this, get help, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

1 answer

3


The best option to deal with this is to store the date in UTC. According to the Moment documentation, unless you set a Timezone, the date is always created in the local Timezone:

Unless you specify a time zone offset, Parsing a string will create a date in the Current time zone - Moment Docs

Thus, storing the date in UTC, when you do the Parsing date on client, it will be correctly converted to the local Timezone. Example:

Storing (backend or frontend):

//Armazena a data como String - 2017-08-07T07:34:49Z
let timestamp = moment.utc().format();

Presentation of the date on frontend:

//Realiza o parsing - 2017-08-07T04:34:49-03:00
let full_timestamp = moment("2017-08-07T07:34:49Z").format();

//07-08-2017 04:34
let formatted = moment("2017-08-07T07:34:49Z").format("DD-MM-YY HH:mm");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.