能在client端写JavaScript,就尽量在客户端写。
格式化时间:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
function formatWithZero(nValue) {
if (nValue < 10 ) nValue = " 0 " + nValue;
return nValue;
}
< / script>
function formatWithZero(nValue) {
if (nValue < 10 ) nValue = " 0 " + nValue;
return nValue;
}
< / script>
ServerSide:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//
Register clientjs and hiddenfield to get client time with format "yyyy/MM/dd hh:mm:ss"
private const string CLIENTTIME_SCRIPT_ID = " __CLIENTTIMEJS " ;
private const string CLIENTTIME_FIELD = " __CLIENTTIME " ;
private string ClientTime
{
get
{
return this .Request.Form[CLIENTTIME_FIELD];
}
}
protected override void OnLoad(EventArgs e)
{
ClientScript.RegisterHiddenField(CLIENTTIME_FIELD, "" );
if ( ! ClientScript.IsOnSubmitStatementRegistered( typeof ( string ), CLIENTTIME_SCRIPT_ID))
{
ClientScript.RegisterOnSubmitStatement( typeof ( string ),
CLIENTTIME_SCRIPT_ID, " var today = new Date();var year =today.getFullYear();var month =today.getMonth()+1; " +
" var day = today.getDate();var hours=today.getHours();var minutes=today.getMinutes();var seconds=today.getSeconds(); " +
" document.getElementById(' " +
CLIENTTIME_FIELD + " ').value=year+'/'+formatWithZero(month)+'/'+formatWithZero(day)+' '+formatWithZero(hours)+':'+formatWithZero(minutes)+':'+formatWithZero(seconds) " );
}
base .OnLoad(e);
}
private const string CLIENTTIME_SCRIPT_ID = " __CLIENTTIMEJS " ;
private const string CLIENTTIME_FIELD = " __CLIENTTIME " ;
private string ClientTime
{
get
{
return this .Request.Form[CLIENTTIME_FIELD];
}
}
protected override void OnLoad(EventArgs e)
{
ClientScript.RegisterHiddenField(CLIENTTIME_FIELD, "" );
if ( ! ClientScript.IsOnSubmitStatementRegistered( typeof ( string ), CLIENTTIME_SCRIPT_ID))
{
ClientScript.RegisterOnSubmitStatement( typeof ( string ),
CLIENTTIME_SCRIPT_ID, " var today = new Date();var year =today.getFullYear();var month =today.getMonth()+1; " +
" var day = today.getDate();var hours=today.getHours();var minutes=today.getMinutes();var seconds=today.getSeconds(); " +
" document.getElementById(' " +
CLIENTTIME_FIELD + " ').value=year+'/'+formatWithZero(month)+'/'+formatWithZero(day)+' '+formatWithZero(hours)+':'+formatWithZero(minutes)+':'+formatWithZero(seconds) " );
}
base .OnLoad(e);
}