十二
02
2009
2

友好的日期及时间显示

使用使用友好的日期显示会有较好的用户体验。非常适合新闻类的网站。
文章发布距当前时间1小时内的,显示n分钟前。
1天内的,显示小时数。
2天和3天的,显示昨天和前天。
7天内的显示,n天前。
其他的显示日期

以下是C#代码:

  1. public static string FriendlyDate(Object sdate)
  2. {
  3.     DateTime date = Convert.ToDateTime(sdate);
  4.     DateTime now = DateTime.Now;
  5.  
  6.     int day = DateTime.Now.Date.Subtract(date).Days;
  7.  
  8.     //1天内
  9.     if (day < 1)
  10.     {
  11.         int hour = (now.Subtract(date)).Hours;
  12.         if (hour > 1)
  13.         {
  14.             return hour + "小时前";
  15.         }
  16.         else
  17.         {
  18.             //1小时内
  19.             int minute = (now.Subtract(date)).Minutes;
  20.             if (minute > 0)
  21.             {
  22.                 return minute + "分钟前";
  23.             }
  24.         }
  25.         return date.ToShortDateString();
  26.     }
  27.     //昨天
  28.     else if (day == 1)
  29.     {
  30.         return "昨天";
  31.     }
  32.     //前天
  33.     else if (day == 2)
  34.     {
  35.         return "前天";
  36.     }
  37.     //4-7天
  38.     else if (day >= 3 && day <= 7)
  39.     {
  40.         return day + "天前";
  41.     }
  42.     else
  43.     {
  44.         return date.ToShortDateString();
  45.     }
  46. }
Written by Nffish in: .Net | 标签:,
十一
25
2009
3

中文字符串转换成十六进制的网址格式

使用中文unicode作网址,第三方控件出现乱码。在我自己的电脑上测试没问题的,到别的机器就有问题了。
一个非常诡异的问题。
估计是编码不一致引起的。
干脆直接把中文转换成了十六进制。

  1. /// <summary>
  2.         /// 中文字符串转换成十六进制的网址格式
  3.         /// </summary>
  4.         /// <param name="str"></param>
  5.         /// <returns></returns>
  6.         public static string String2Hex(string str)
  7.         {
  8.             //把字符串放到数组里
  9.             char[] c = new char[str.Length];
  10.             for (int i = 0; i < str.Length; i++)
  11.             {
  12.                 c[i] = str[i];
  13.             }
  14.  
  15.  
  16.             //注意编码,根据需要GB2312或UTF-8
  17.             Byte[] textByte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(c);
  18.  
  19.             StringBuilder text = new StringBuilder();
  20.             for (int j = 0; j < textByte.Length; j++)
  21.             {
  22.                 // to char
  23.                 char textChar = Convert.ToChar(int.Parse(textByte[j].ToString()));
  24.                 // to Hex
  25.                 text.Append(System.Uri.HexEscape(textChar));
  26.             }
  27.             return text.ToString();
  28.         }
Written by Nffish in: .Net | 标签:, ,

16
2009
2

ASP.NET中使用AJAX

.NET Framework 3.5和2.0都可以用VS2008,VS2005测试通过

1.引用Ajax.dll文件
2.配置Web.config
<configSections>节点添加
<httpHandlers>节点添加
3.在需要使用的aspx页面的Page_Load事件中添加
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxDemo));
AjaxDemo是当前这个文件的类名

4.在需要被客户端调用的后台方法前添加AjaxMethod注释

[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public string[] GetCustomerMac(string clientIP)


5.aspx页面js

//触发事件
function Button1_onclick() {
var ip=document.getElementById(“ip”).value;
AjaxDemo.GetCustomerMac(ip,CustomerMacCallBack);
}

//回调方法
function CustomerMacCallBack(result)
{
var data=result.value;
document.getElementById(“Mac”).value=data;
}

Written by Nffish in: .Net | 标签:,

05
2008
2

用C#实现MD5加密

昨晚睡不着,用手机上Google Reader看到关于Hash的文章,突然想到了MD5加密,最近在学.Net平台,所以想整个C#写的Md5加密方法出来。

Google了一下,再结合一些自己的思想。

MD5加密

环境:Windows xp sp3 + vs2008

下载源代码

Written by Nffish in: .Net | 标签:, , ,

Powered By Wordpress Theme AEROTRONIC |
除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。