将纪元日期时间文本框值转换为 24 小时人类日期?
Turn epoch datetime textbox value to a 24h human date?
我有一个包含纪元日期时间戳的第一个文本框。
因为这种格式不是人类可读的,我决定将它设置为只读和隐藏。
根据第一个文本框值,我想将其纪元值映射到 24 小时人类日期,该日期将在 2sd 文本框中可见。
这是我目前所做的,但它不起作用:
( ~gpio_actual_schedule_event~ 在页面加载时由服务器自动替换为日期时间戳)
HTML
<input type="hidden" value="~gpio_actual_schedule_event~" id="epoch_stamp" readonly >
<input type="text" id="converted_set_next_event" readonly >
在页面加载时,我使用 Javascript 将日期时间戳转换为人类可读格式:
<script>
function epoch_to_24h_date(){
var epochdate = new Date(~gpio_actual_schedule_event~ * 1); // multiply by 1000 for milliseconds
var date_string = epochdate.toLocaleString('en-GB'); // 24 hour format
}
</script>
<script>
window.onload= function(){
epoch_to_24h_date();
document.getElementById('converted_set_next_event').value = date_string;
}
</script>
date_string
变量仅在 epoch_to_24h_date
函数内可用。你应该 return 它:
<script>
function epoch_to_24h_date(){
var epochdate = new Date(~gpio_actual_schedule_event~ * 1); // multiply by 1000 for milliseconds
return epochdate.toLocaleString('en-GB'); // 24 hour format
}
</script>
<script>
window.onload= function(){
document.getElementById('converted_set_next_event').value = epoch_to_24h_date();
}
</script>
我有一个包含纪元日期时间戳的第一个文本框。
因为这种格式不是人类可读的,我决定将它设置为只读和隐藏。
根据第一个文本框值,我想将其纪元值映射到 24 小时人类日期,该日期将在 2sd 文本框中可见。
这是我目前所做的,但它不起作用:
( ~gpio_actual_schedule_event~ 在页面加载时由服务器自动替换为日期时间戳)
HTML
<input type="hidden" value="~gpio_actual_schedule_event~" id="epoch_stamp" readonly >
<input type="text" id="converted_set_next_event" readonly >
在页面加载时,我使用 Javascript 将日期时间戳转换为人类可读格式:
<script>
function epoch_to_24h_date(){
var epochdate = new Date(~gpio_actual_schedule_event~ * 1); // multiply by 1000 for milliseconds
var date_string = epochdate.toLocaleString('en-GB'); // 24 hour format
}
</script>
<script>
window.onload= function(){
epoch_to_24h_date();
document.getElementById('converted_set_next_event').value = date_string;
}
</script>
date_string
变量仅在 epoch_to_24h_date
函数内可用。你应该 return 它:
<script>
function epoch_to_24h_date(){
var epochdate = new Date(~gpio_actual_schedule_event~ * 1); // multiply by 1000 for milliseconds
return epochdate.toLocaleString('en-GB'); // 24 hour format
}
</script>
<script>
window.onload= function(){
document.getElementById('converted_set_next_event').value = epoch_to_24h_date();
}
</script>