Help with getTimeZoneOffset() JS function in Flask

Question:
I am trying to adjust the times of the comments accordingly to the user’s timezone before displaying them. I know that the getTimezoneOffset() JS function can get the user’s time from UTC in minutes, but I am not sure how to use Flask to render the HTML with the timezone adjusted to the return of the JS through the Python code.

Repl link:
https://replit.com/@MiloCat/PianoMan0


from flask import Flask, render_template, request, redirect
from replit import db
import datetime

app = Flask(__name__)

@app.route('/')
def index():
    user_name = request.headers.get('X-Replit-User-Name')
    comments = db.get('comments') or []
    return render_template('index.html', user_name=user_name, comments=comments)

@app.route('/add_comment', methods=['POST'])
def add_comment():
    user_name = request.headers.get('X-Replit-User-Name')
    pfp = request.headers.get('X-Replit-User-Profile-Image')
    comment = request.form['comment']
    comments = db.get('comments') or []
    now = datetime.datetime.now()
    formatted_date = now.strftime("%B %d, %Y at %I:%M %p")
    comments.append((user_name, comment, pfp, formatted_date))
    db['comments'] = comments
    return redirect("/")

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=81)

Use js to change it front-end, don’t bother with making it flask.

Wha do you mean?


I mean use js instead of flask for this. In this case it is probably more efficient.

Yes but I don’t know js :frowning:

You literally cannot do any substantial web development without learning js. I recommend learning the basic, you don’t really need to delve to deep.

I already know that but rn I don’t have the motivation to learn js (I know a little bit at least)

On line 74 in your html, change it to this first:


<strong>{{ comment[0] }} - <span class="time">{{ comment[3] }}</span></strong></div>

That doesn’t change anything :0

Yeah, because you did not add js yet.

document.querySelectorAll(".time").forEach((item) => {
   let d = new Date(item.textContent)
   item.textContent = d.toString()
});

That should work as long as you have UTC at the end. (And make sure it is a ISO-8601 formatted date). So do not format your date.

What do you mean UTC at the end? The date is already in UTC

Then it will be fine.

Internal server error when trying to submit a comment when i stopped converting it, circular reference

Can you provide the full error? I can’t really help without it lol

It’s very long, can I invite?

Edit: invited

1 Like

The solution we came up with is this:

var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification));

if (!isSafari) {
  document.querySelectorAll(".time").forEach((item) => {
    // console.log(Date.parse(item.textContent));
    let d = new Date(Date.parse(item.textContent));
    // console.log(d);
    item.textContent = d.toLocaleString();
  });
}

Note: dates on iOS safari are broken, that is why there are extra stuff

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.