用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
- 用Int存储时间 解决新闻订单等的 排序问题
- 关于一些解决客户订单的问题
- 用视图+存储过程解决复杂查询的排序分页问题
- mysql 用case when 解决比较时间大小后在排序的问题
- 解决在创建销售订单时出现工厂日历CN等错误提示问题
- 解决magento模板下订单没有“下单按钮”,“提交订单按钮”的问题【10】
- ES存储的时间问题
- 解决整数,货币,INT,DOUBLE,等类型的JavaScript验证
- short int,int,unsigned int,long,long int,float,double等32位,64位各占几个字节的问题
- 解决了新闻修改和新闻显示乱码冲突的问题
- 解决一个mysql关于按文章以及评论的最早时间的排序问题
- 订单添加 批量删除 搜索 排序 时间排序 物流状态
- C语言的int (*a)[10]等指针问题理解
- 用哈希算法的思想解决排序和字符串去重问题,时间复杂度为O(N)
- ecshop怎样解决重复订单问题
- Magento通过订单id修改订单创建时间的sql
- 解决tomcat 等一些服务端口被占用的问题
- mysql里面存储时间的问题
- win7设置防火墙允许Ping与telnet
- js操作XML/HTML常用的对象属性
- Quoit Design(最近点对问题)
- c++学习笔记(十六):函数模板
- 一台机器同时部署运行多个Tomcat
- 用Int存储时间 解决新闻订单等的 排序问题
- tftp命令
- 同行不同命:中美两国的“网络水军”有哪些差异
- 如何将打印出来的内容输出到文本或excel当中
- IBM知识库
- hashCode() 和equals() 区别和作用
- 登陆页面CSS
- 兼容多浏览器,兼容 div、p、td 的强制不换行及强制换行
- 导入 jar 包及第三方开源库教程