将日期转换为int foramt [英] Converting date to int foramt

查看:69
本文介绍了将日期转换为int foramt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将2013/12/03转换为20131203



我在C#代码中使用日历控件,我需要将'date'格式保存到'int '在我的数据库中。



如果有人能给我一些关于怎么做的答案会非常充实。



谢谢,

解决方案

最佳做法是在数据库中将日期存储为DATE数据类型。通过将日期存储为整数,您将使用日期必须完成的所有处理复杂化并增加编程错误的机会。请重新考虑使用INT数据类型作为您的日期。 DATE数据类型不占用额外空间,并提供易用性和防止常见逻辑错误。


  string  datestr =   2013/12/03; 
// 简单方法
int dateInt = Convert.ToInt32(datestr.Replace( / ));

// (或)
// 使用日期对象
DateTime dt = DateTime.ParseExact(datestr, yyyy / MM / dd,System.Globalization.CultureInfo.InvariantCulture);
int dateInt2 = Convert.ToInt32(dt.ToString( YYYYMMDD));


使用DateTime的Ticks属性 - 它比20131203格式更好,因为它可以用来重现日期,而不知道如何格式化。

How to convert 2013/12/03 into 20131203

I am using calender control in my C# code and i need to save 'date' format into 'int' in my DB.

If anyone can give me some answers on how to do will be very great full.

Thanks,

解决方案

It is a best practice to store dates as DATE data type in the database. By storing the date as an integer, you complicate all processing that must be done with the date and increase the chance of programming errors. Please reconsider using INT data type for your date. The DATE data type takes no additional space and provides ease of use and protection from common logic errors.


string datestr = "2013/12/03";
       // simple way is
       int dateInt = Convert.ToInt32(datestr.Replace("/", ""));

       // ( or )
       // using date object
       DateTime dt = DateTime.ParseExact(datestr, "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
       int dateInt2 = Convert.ToInt32(dt.ToString("yyyyMMdd"));


Use DateTime's Ticks property - its better than 20131203 format as it can be used to reproduce the date without knowledge of how to formatting was done.


这篇关于将日期转换为int foramt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆