[2024-10-12] Get user local time using Javascript

This commit is contained in:
Andrew Conlin 2024-10-12 16:21:53 +05:30
parent 7356b00de1
commit 13ec41e8a4
2 changed files with 33 additions and 7 deletions

18
templates/init.html Normal file
View File

@ -0,0 +1,18 @@
<html>
<div style="visibility: hidden">
<form id="form" method="POST" action="/">
<input name="timestamp" id="timestamp" type="number"></input>
</form>
</div>
</html>
<script>
window.onload = function(e){
var d = new Date();
var timestamp = Math.round(d.getTime() / 1000);
input = document.getElementById("timestamp")
input.value = timestamp;
form = document.getElementById("form")
form.submit();
console.log(time);
}
</script>

View File

@ -1,7 +1,7 @@
import datetime
import math
from flask import Flask, render_template, url_for
from flask import Flask, render_template, url_for, request
app = Flask(__name__)
def num2day(num):
@ -136,8 +136,8 @@ def getHomoSapiens(yearPercent):
else:
return f"Homo Sapiens have been around for ~{-math.floor(yearsUntilHumans)} years."
def getTemplate():
now = datetime.datetime.now()
def getTemplate(timestamp):
now = datetime.datetime.fromtimestamp(timestamp)
nowTime = now.strftime("%-I:%M%p")
nowDate = now.strftime("%A") + " " + now.strftime("%-d") + " " + now.strftime("%B")
@ -155,10 +155,9 @@ def getTemplate():
return now.year, nowTime, nowDate, round(yearPercent*100,2), footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens
@app.route("/")
def index():
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate()
cssPath = url_for('static', filename='style.css')
def index(timestamp):
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp)
cssPath = url_for("static", filename="style.css")
return render_template("index.html",\
cssPath=cssPath,\
year=year,\
@ -174,3 +173,12 @@ def index():
lat=lat,\
long=long,\
homoSapiens=homoSapiens)
@app.route("/", methods=["GET","POST"])
def init():
if request.method == "POST":
timestamp = int(request.values.get('timestamp'))
return index(timestamp)
else:
return render_template("init.html")