using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XWP_KZ
{
public static class KZclassHelper
{
/// <summary>
/// string 转换 int 错误默认返回:0
/// </summary>
/// <param name="numb"></param>
/// <returns>错误返回: [error] ,成功返回: [numb]</returns>
public static int ToInt(this string numb)
{
int num = 0;
if (int.TryParse(numb, out num))
return num;
else
return 0;
}
/// <summary>
/// string 转换 int
/// </summary>
/// <param name="numb"></param>
/// <param name="error">错误返回int</param>
/// <returns>错误返回: [error] ,成功返回: [numb]</returns>
public static int ToInt(this string numb,int error)
{
int num = 0;
if (int.TryParse(numb, out num))
return num;
else
return error;
}
/// <summary>
/// string 转换 dateTime
/// </summary>
/// <param name="dateTime"></param>
/// <returns>错误返回:最小时间 ,成功返回:[时间]</returns>
public static DateTime ToDateTime(this string dateTime)
{
DateTime dt;
if (DateTime.TryParse(dateTime, out dt))
return dt;
else
return DateTime.MinValue;
}
/// <summary>
/// string 转换 dateTime
/// </summary>
/// <param name="dateTime"></param>
/// <param name="error"></param>
/// <returns>错误返回:[error] ,成功返回:[dateTime]</returns>
public static DateTime ToDateTime(this string dateTime,DateTime error)
{
DateTime dt;
if (DateTime.TryParse(dateTime, out dt))
return dt;
else
return error;
}
/// <summary>
/// 时间戳 转换 时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns>返回:[DateTime]</returns>
public static DateTime ToDateTime(this int timeStamp)
{
DateTime a = DateTime.Now;
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
/// <summary>
/// 时间 转换 时间戳
/// </summary>
/// <param name="time"></param>
/// <returns>返回:[int]</returns>
public static int ToDateTimeStamp(this DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
}
}
使用方法
1、引用 using XWP_KZ
具体查看msdn扩展方法