用Int存储时间 解决新闻订单等的 排序问题

来源:互联网 发布:sdh网络单元主要有 编辑:程序博客网 时间:2024/05/19 20:41

     最近的在改版新闻系统发现,新闻排序结果不准确,原因在于直接存入数据库时存的是字符串,如2015-10-27 ,新版格式和旧版不一样,于是经过讨论决定使用Int存储时间,这样排序就准确了!

     下面是帮助类

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace ServicePlatform.Lib{    public class DateTimeFormat    {        /// <summary>        /// 将Unix时间戳转换为DateTime类型时间        /// </summary>        /// <param name="d">int 型数字</param>        /// <returns>DateTime</returns>        public static System.DateTime ConvertIntDateTime(int d)        {            System.DateTime time = System.DateTime.MinValue;            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));            time = startTime.AddSeconds(d);            return time;        }        /// <summary>        /// 将c# DateTime时间格式转换为Unix时间戳格式        /// </summary>        /// <param name="time">时间</param>        /// <returns>int</returns>        public static int ConvertDateTimeInt(System.DateTime time)        {            int intResult = 0;            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));            intResult = (int)(time - startTime).TotalSeconds;            return intResult;        }        /// <summary>        /// 将输入的年月日 转换为DateTime类型时间,如果输入不合法则返回 DateTime.MinValue 【年月日-->年—月-日】-->DateTime-->Int        /// </summary>        /// <param name="Y">年</param>        /// <param name="M">月</param>        /// <param name="D">日</param>        /// <returns>DateTime</returns>        public static System.DateTime ChekInputDate(string Y, string M, string D)         {             int year=0;             int month = 0;             int day =0;            //输入的为数字,并且满足要求的范围              if ( int.TryParse(Y,out year)&& int.TryParse(M, out month)&& int.TryParse(D, out day) &&year >= 1970 && year <= 2050 && month >= 1 && month <= 12 && day >= 1 && day <= 31)             {                 return TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(year, month, day));             }             else             {                 return DateTime.MinValue;             }         }        public static System.DateTime ChekInputDate(string DateString)        {            if(DateString.Length==8)            {                string Y = DateString.Substring(0, 4);                string M = DateString.Substring(4, 2);                string D = DateString.Substring(6, 2);                int year = 0;                int month = 0;                int day = 0;                //输入的为数字,并且满足要求的范围                 if (int.TryParse(Y, out year) && int.TryParse(M, out month) && int.TryParse(D, out day) && year >= 1970 && year <= 2050 && month >= 1 && month <= 12 && day >= 1 && day <= 31)                {                    return TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(year, month, day));                }                else                {                    return DateTime.MinValue;                }            }                       else            {                return DateTime.MinValue;            }        }     }}

0 0
原创粉丝点击