js 计算时间差与asp.net结合示例

发布时间:2019-08-25编辑:脚本学堂
有关js计算时间差的一段代码,在asp.net中获取结果,计算日期时间差值,不了解的朋友参考下。

代码:
 

复制代码 代码示例:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title>js 计算时间差 - www.jb200.com</title>
<script type="text/javascript"> 
function aa() { 
alert('ok'); 

</script> 
</head> 
<body onload="getvalue()"> 
<form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
<div id="publish"> 
<asp:TextBox ID="txtSay" runat="server" Height="117px" TextMode="MultiLine" Width="359px"></asp:TextBox><br /> 
</div> 
<div id="MList"> 
浏览总数:<asp:Label ID="Ltatol" runat="server" Text="Label" Font-Bold="True" Font-Size="Larger" 
ForeColor="#CC0000"></asp:Label> 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  
Width="884px" ondatabound="GridView1_DataBound"  
onrowdatabound="GridView1_RowDataBound"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<table class="style1"> 
<tr> 
<td style="background-color: #eee;"> 
昵称:<%# Eval("username") %></td> 
</tr> 
<tr> 
<td style="background-color: Aqua;"> 
内容:<%# Eval("content") %></td> 
</tr> 
<tr> 
<td> 
时间:<%# Eval("timespan")%></td> 
</tr> 
</table> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 
</div> 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="link1" EventName="Click" /> 
<asp:AsyncPostBackTrigger ControlID="link2" EventName="Click" /> 
<asp:AsyncPostBackTrigger ControlID="link3" EventName="Click" /> 
<asp:AsyncPostBackTrigger ControlID="link4" EventName="Click" /> 
</Triggers> 
</asp:UpdatePanel> 
<%-- this is nodisplay--%> 
<div> 
<asp:LinkButton ID="link1" runat="server" OnClick="LinkButton1_Click" >第一页</asp:LinkButton> 
<asp:LinkButton ID="link2" runat="server" OnClick="LinkButton2_Click" >下一页</asp:LinkButton> 
<asp:LinkButton ID="link3" runat="server" OnClick="LinkButton3_Click">上一页</asp:LinkButton> 
<asp:LinkButton ID="link4" runat="server" OnClick="LinkButton4_Click">末页</asp:LinkButton> 
</div> 
 <p> 
   根据《中华人民共和国反倾销条例》的规定,商务部 
   根据《中华人民共和国反倾销条例》的规定,商务部<br /> 
(以下称调查机关) 2010年12月23日发布2010年<br /> 
第93号公告,决定对原产于欧盟、美国和日<br /> 
本的进口相纸产品(以下简称被调查产品)<br /> 
进行反倾销立案调查。 
 </p> 
<script type="text/javascript"> 
   function getvalue() { 

   var sen = 1000; 
   var minute = sen * 60; 
   var hour = minute * 60; 
   var day = hour * 24; 
   var month = day * 30; 

   var mytable = document.getElementById("GridView1"); 
 
   for (var i = 1; i < mytable.rows.length; i++) { 
   var mycellnow = mytable.rows[i].cells[0].childNodes[0].rows[2].cells[0]; 
   var diffVlaue = mycellnow.innerText; 
 
   diffVlaue1 = diffVlaue.substring(3); 
   //alert(diffVlaue+'========'+diffVlaue1); 
   diffVlaue1 = parseInt(diffVlaue1); 
   var monthC = diffVlaue1/month; 
   var weekC = diffVlaue1/(7 * day); 
   var dayC = diffVlaue1/day; 
   var hourC = diffVlaue1/hour; 
   var minC = diffVlaue1/minute; 
 var mc=diffVlaue1/sen ; 
 
   if (monthC >= 1) { 
   mycellnow.innerText = "发表于" + parseInt(monthC) + "个月前"; 
   } 
   else if (weekC >= 1) { 
   mycellnow.innerText = "发表于" + parseInt(weekC) + "个星期前"; 
   } 
   else if (dayC >= 1) { 
   mycellnow.innerText = "发表于" + parseInt(dayC) + "天前"; 
   } 
   else if (hourC >= 1) { 
   mycellnow.innerText = "发表于" + parseInt(hourC) + "个小时前"; 
   } 
   else if (minC >= 1) { 
   mycellnow.innerText = "发表于" + parseInt(minC) + "分钟前"; 
   } 
   else {
   mycellnow.innerText = "发表于" + parseInt(senC) + "秒前"; 
   } 
   } 
   } 
</script> 
</form> 
</body> 
</html>  
 

 
后台代码: 
 

复制代码 代码示例:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Threading; 
 
public partial class _Default : System.Web.UI.Page 

userInfo uinfo; 
protected void Page_Load(object sender, EventArgs e) 

// this.Button1.Attributes.Add("onclick", "getvalue()"); 
if (Session["userinfo"] != null) 

uinfo = Session["userinfo"] as userInfo; 

 
if (!IsPostBack) 

Session["pageindex"] = "1"; 
bindGridview(); 
initAllmessage(); 
//分页代码 


 
//评论总条数方法 
private void initAllmessage() 

string str = "server=FJM-PCSQLEXPRESS;initial catalog=PingLun;integrated security=true"; 
object sum = null; 
using (SqlConnection con = new SqlConnection(str)) 

using (SqlCommand com = con.CreateCommand()) 

com.CommandText = "select count(id) from PingLun"; 
con.Open(); 
sum = com.ExecuteScalar().ToString(); 
 


if (sum != null) 

this.Ltatol.Text = "" + sum.ToString();  //记录总共有多少条评论 

else 

this.Ltatol.Text = "暂无评论";  //暂无 


 
private void bindGridview() 

//Thread.Sleep(2000); 
string str = "server=FJM-PCSQLEXPRESS;initial catalog=PingLun;integrated security=true"; 
DataTable dt = null; 
using (SqlConnection con = new SqlConnection(str)) 

using (SqlCommand com = con.CreateCommand()) 

com.CommandText = "fenyePinglun"; 
com.CommandType = CommandType.StoredProcedure; 
com.Parameters.AddWithValue("@pageindex",Convert.ToInt32(Session["pageindex"])); 
com.Parameters.AddWithValue("@pagesize", 4); 
com.Parameters.Add("@pagecount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output; 
SqlDataAdapter da = new SqlDataAdapter(com); 
dt = new DataTable(); 
da.Fill(dt); 
ViewState["pagecount"] = com.Parameters["@pagecount"].Value; 
//ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ViewState["pagecount"] + "')", true); 


 
this.GridView1.DataSource = dt; 
this.GridView1.DataBind(); 
 

protected void btnshow_Click(object sender, EventArgs e) 

if (Session["userinfo"] == null) 

Response.Redirect("Denglu.aspx"); 

else 

string mysays = this.txtSay.Text; 
if (mysays.Length > 300) 

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('您输入的字数不得超过300字')</script>", false); 

else 

insertMessages(); 


 

 
//发表评论 
private void insertMessages() 

string str = "server=FJM-PCSQLEXPRESS;initial catalog=PingLun;integrated security=true"; 
int i = 0; 
using (SqlConnection con = new SqlConnection(str)) 

using (SqlCommand com = con.CreateCommand()) 

com.CommandText = "insert into PingLun(userID,content) values(@userID,@content)"; 
com.Parameters.AddWithValue("@userID", "1"); 
com.Parameters.AddWithValue("@content", this.txtSay.Text); 
con.Open(); 
i = com.ExecuteNonQuery(); 


if (i == 1) 

this.txtSay.Text = ""; 
bindGridview(); 
initAllmessage(); 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('评论成功')</script>", false); 

else 

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('评论失败')</script>", false); 


protected void LinkBtn1_Click(object sender, EventArgs e) 

 

protected void LinkButton1_Click(object sender, EventArgs e) 

Session["pageindex"] = "1"; 
bindGridview();  //显示第一页 
OperScript(); 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Session["pageindex"] + "')", true); 

protected void LinkButton2_Click(object sender, EventArgs e) 

int pageindex = Convert.ToInt32(Session["pageindex"]); 
int pagecount = Convert.ToInt32(ViewState["pagecount"]); 
if (pageindex < pagecount) 

pageindex++;  

Session["pageindex"] = pageindex.ToString(); 
bindGridview();  //显示下一页 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Session["pageindex"].ToString() + "')", true); 
 
 
OperScript(); 
 

protected void LinkButton3_Click(object sender, EventArgs e) 

int pageindex = Convert.ToInt32(Session["pageindex"]); 
 
if (pageindex > 1) 

pageindex--; 
Session["pageindex"] = pageindex.ToString(); 

bindGridview();  //显示上一页 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Session["pageindex"] + "')", true); 
 
OperScript(); 
 

protected void LinkButton4_Click(object sender, EventArgs e) 

 
int pagecount = Convert.ToInt32(ViewState["pagecount"]); 
Session["pageindex"] = pagecount.ToString(); 
bindGridview();  //显示最后一页 
OperScript(); 
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + Session["pageindex"] + "')", true); 
 

protected void Button1_Click(object sender, EventArgs e) 

 

protected void btn1_Click(object sender, EventArgs e) 

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 


protected void GridView1_DataBound(object sender, EventArgs e) 

//ClientScript.RegisterStartupScript(ClientScript.GetType(), 
//"myscript", "<script>aa();</script>");  

 
protected void OperScript() 

ScriptManager.RegisterStartupScript(this, this.GetType(), "asdf", "getvalue()",true); 
}