如何将当前日期和时间放在 PHP Javascript 中的文本框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17511063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put current date and time inside textbox in PHP Javascript
提问by gimmigzgy
I am trying to put in my textbox the current time, with my preferred format (ex. 14:25:30) What's wrong with my code? because when I run it, the textbox appears empty.
我正在尝试使用我喜欢的格式(例如 14:25:30)将当前时间放入我的文本框中 我的代码有什么问题?因为当我运行它时,文本框显示为空。
<td>
<input id="date" name="curTime" size="20" maxlength="20" Required />
</td>
<script type="text/javascript">
var timenow = new Date();
timenow.format("UTC:h:MM:ss TT Z");
document.getElementById("date").value = new Date().toUTCString();
</script>
回答by Andy G
There is no formatfunction for a Date(), unless you have written one yourself. In which case, the error is probably in this function.
Date()没有格式函数,除非您自己编写。在这种情况下,错误可能出在这个函数中。
The format() call creates an error, but you are not using timenow
anyway. I assume you intended to set the value to timenow.
format() 调用会产生错误,但timenow
无论如何您都没有使用。我假设您打算将该值设置为 timenow。
You could borrow my format method, but it doesn't include UTC, TT or Z.
你可以借用我的格式方法,但它不包括 UTC、TT 或 Z。
Date.prototype.format = function (sFormat, twelve) {
// Returns: A string version of the date.
// Usage: date_instance.format("d mmm yy hh:nn:ss ap") or
// date_instance.format("dddd dd mmmm hh:nn", true)
// Defaults to YYYY/MM/DD.
// twelve == true for a 12hr clock, or just AP or ap within
// sFormat (for AM/PM or am/pm).
// Use z or zzz for milliseconds and xx for suffixes (st, nd, etc.).
var MonthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var DayNames = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday",
"Friday", "Saturday" ];
var dDate = this || new Date(),
D = dDate.getDate(), DDDD = DayNames[dDate.getDay()],
DDD = DDDD.substr(0,3),
M = dDate.getMonth()+1, MMMM = MonthNames[dDate.getMonth()],
MMM = MMMM.substr(0,3),
YYYY = dDate.getFullYear(), YY = ('' + YYYY).substr(2, 2),
H = dDate.getHours(), N = dDate.getMinutes(), S = dDate.getSeconds(),
Z = dDate.getMilliseconds(),
ap = (H > 11) ? "pm" : "am",
// pad with leading zeros, if required
DD = ( D < 10 ? "0" : "" ) + D,
MM = ( M < 10 ? "0" : "" ) + M,
NN = ( N < 10 ? "0" : "" ) + N,
SS = ( S < 10 ? "0" : "" ) + S,
ZZZ = ( Z < 10 ? "00" : (Z < 100 ? "0" : "") ) + Z, XX;
var AP = (sFormat && (sFormat.toUpperCase().indexOf('AP')+1)) ?
((sFormat.indexOf('ap')+1) ? ap : ap.toUpperCase()) : '';
if (twelve || AP) {
H = (H < 12) ? (H || 12) : ((H - 12) || 12);
}
var HH = ( H < 10 ? "0" : "" ) + H;
XX = (D == 1 || D == 21 || D == 31) ? "st" :
((D == 2 || D == 22) ? "nd" : ((D == 3 || D == 23) ? "rd" : "th"));
sFormat = ( sFormat ) ? sFormat.toUpperCase() : 'YYYY/MM/DD';
var sParsed = sFormat.replace(/D{1,4}|M{1,4}|Y{2,4}|H{1,2}|N{1,2}|S{1,2}|Z{1,3}|XX|AP/g,
function (m) {
try {
return eval(m);
} catch (e) {
return '';
}
});
return sParsed;
};
I use 'nn' for minutes, reserving 'mm' for the month.
我使用 'nn' 表示分钟,保留 'mm' 表示月份。
回答by Jay Harris
change your javascript to this.
将您的 javascript 更改为此。
var timenow = new Date();
document.getElementById("date").value = timenow.toString();
the format function format()
is a php thing, to print the date as a string in the text box use the toString()
function.
format 函数format()
是 php 的东西,要在文本框中将日期打印为字符串,请使用该toString()
函数。
you can set the format of the date in the toString()
function
您可以在toString()
函数中设置日期的格式
回答by RobG
You can format the current UTC time in javascript using a function like:
您可以使用如下函数在 javascript 中格式化当前的 UTC 时间:
function getCurrentUTCTime() {
var d = new Date();
function z(n){ return (n<10? '0':'') + n;}
return z(d.getUTCHours()) + ':' +
z(d.getUTCMinutes()) + ':' +
z(d.getUTCSeconds());
}
For local time:
当地时间:
function getCurrentTime() {
var d = new Date();
function z(n){ return (n<10? '0':'') + n;}
return z(d.getHours()) + ':' +
z(d.getMinutes()) + ':' +
z(d.getSeconds());
}
回答by Ritz
You just need to change your javascript(24 hour format)
您只需要更改您的 javascript(24 小时格式)
<script>
{
var today=new Date();
var t=today.toLocaleTimeString("en-GB");
document.getElementById("date").value=t;
}
</script>
Hope this helps!!
希望这可以帮助!!
回答by Moeed Farooqui
You can also do this with builtin functions of PHP
您也可以使用 PHP 的内置函数执行此操作
<input id="date" name="curTime" value="<?php echo date('D-M-Y')." ".gmdate("H:i:s", time()); ?>" size="20" maxlength="20" Required />