You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ICSSoft.Frame.Helper { public class FormatHelper { public const char TRUE = '1'; public const char FALSE = '0'; public const string TRUE_STRING = "1"; public const string FALSE_STRING = "0";
/// <summary>
/// 格式化数据库时间
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static string ToTimeString(int time) { return ToTimeString(time, ":"); }
/// <summary>
/// 只显示时分,不显示秒
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static string ToShortTimeString(int time) { string timeString = time.ToString().PadLeft(6, '0'); string timeSplitChar = ":"; return string.Format("{0}{1}{2}" , timeString.Substring(0, 2) , timeSplitChar , timeString.Substring(2, 2)); }
/// <summary>
/// 格式化数据库时间
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static string ToTimeString(int time, string timeSplitChar) { string timeString = time.ToString().PadLeft(6, '0');
return string.Format("{0}{1}{2}{3}{4}" , timeString.Substring(0, 2) , timeSplitChar , timeString.Substring(2, 2) , timeSplitChar , timeString.Substring(4, 2)); }
public static int TOTimeInt(System.DateTime dateTime) { if (dateTime <= DateTime.MinValue) { return 0; }
return dateTime.Hour * 10000 + dateTime.Minute * 100 + dateTime.Second; }
public static int TOTimeInt(string time) { if (time == null || time.Trim() == string.Empty) { return 0; }
char[] split = new char[] { ':' }; string[] array = time.Split(split);
return System.Int32.Parse(array[0]) * 10000 + System.Int32.Parse(array[1]) * 100 + System.Int32.Parse(array[2]); }
public static int TODateInt(System.DateTime dateTime) { if (dateTime <= DateTime.MinValue) { return 0; }
return dateTime.Year * 10000 + dateTime.Month * 100 + dateTime.Day;
}
public static int TODateInt(string Date) { if (Date == null || Date.Trim() == string.Empty) { return 0; } char[] split = new char[2]; split[0] = '/'; split[1] = '-'; string[] array = null; array = Date.Split(split); return System.Int32.Parse(array[0]) * 10000 + System.Int32.Parse(array[1]) * 100 + System.Int32.Parse(array[2]); }
/// <summary>
/// 获取一年第几周
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static int GetRecentWeekOfYear() { int weekOfYear = 0;
int days = DateTime.Now.DayOfYear;
int dayofweek = 0;
switch (DateTime.Now.DayOfWeek) { case DayOfWeek.Sunday: dayofweek = 1; break; case DayOfWeek.Monday: dayofweek = 2; break; case DayOfWeek.Tuesday: dayofweek = 3; break; case DayOfWeek.Wednesday: dayofweek = 4; break; case DayOfWeek.Thursday: dayofweek = 5; break; case DayOfWeek.Friday: dayofweek = 6; break; case DayOfWeek.Saturday: dayofweek = 7; break; default: dayofweek = 0; break; }
weekOfYear = (days - dayofweek) / 7 + 1;
return weekOfYear; }
/// <summary>
/// 获取一年第几周
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static int GetRecentWeekOfYear(DateTime dt) { int weekOfYear = 0; int days = dt.DayOfYear;
int dayofweek = 0; switch (DateTime.Now.DayOfWeek) { case DayOfWeek.Sunday: dayofweek = 1; break; case DayOfWeek.Monday: dayofweek = 2; break; case DayOfWeek.Tuesday: dayofweek = 3; break; case DayOfWeek.Wednesday: dayofweek = 4; break; case DayOfWeek.Thursday: dayofweek = 5; break; case DayOfWeek.Friday: dayofweek = 6; break; case DayOfWeek.Saturday: dayofweek = 7; break; default: dayofweek = 0; break; }
weekOfYear = (days - dayofweek) / 7 + 1;
return weekOfYear; }
public static bool StringToBoolean(string value) { return value == TRUE_STRING; }
/// <summary>
/// 返回在指定字符串位置上为1,则返回true,0返回false
/// </summary>
/// <param name="orginalString">指定字符串</param>
/// <param name="index">位置索引</param>
/// <returns></returns>
public static bool StringToBoolean(string orginalString, int index) { if ((index > orginalString.Length) || (index < 0)) { throw new Exception("$Error_Index_Out_Of_Range"); } return StringToBoolean(orginalString[index].ToString()); }
public static bool StringToBoolean(string value, string trueValue) { return value == trueValue; }
public static string BooleanToString(bool value) { return value ? TRUE_STRING : FALSE_STRING; }
public static string BooleanToGirdCheckBoxString(char value) { return value == TRUE ? "true" : "false"; }
public static string BooleanToString(string orginalString, int index, bool value) { if ((index > orginalString.Length) || (index < 0)) { throw new Exception("$Error_Index_Out_Of_Range"); } char[] tmpChar = orginalString.ToCharArray(); tmpChar[index] = value ? TRUE : FALSE; return new string(tmpChar); }
} }
|