不完整日期策略 [英] Strategy for Incomplete Dates

查看:130
本文介绍了不完整日期策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们希望用户输入不完整日期的应用程序中进行工作。在某些情况下,只有一年 - 例如1854年,或者可能有一年和一个月,例如1983年3月,或者可能有一个完整的日期 - 2001年6月11日。



我们希望使用一个date属性/列,并能够在日期排序。



任何建议?

解决方案

将日期存储为整数 - - yyyymmdd。



然后,您可以将任何未输入的任何月份或日期组件清零

 仅年:1954 => 19540000 
年月份:2004年4月=> 20040400
2011年1月1日=> 20110101

当然,我假设您不需要存储任何时间的日期信息。 p>

然后,您可以创建一个结构来将此逻辑与包含已设置的粒度级别的有用属性相关联,相关的System.DateTime等。



编辑:排序应该很好地工作


Working on an application where we would like the user to be able to enter incomplete dates.

In some cases there will only be a year - say 1854, or there might be a year and a month, for example March 1983, or there may be a complete date - 11 June 2001.

We'd like a single 'date' attribute/column - and to be able to sort on date.

Any suggestions?

解决方案

Store the date as an integer -- yyyymmdd.

You can then zero out any month or day component that has not been entered

Year only: 1954 => 19540000
Year & Month: April 2004 => 20040400
January 1st, 2011 => 20110101

Of course I am assuming that you do not need to store any time of day information.

You could then create a struct to encapsulate this logic with useful properties indicating which level of granularity has been set, the relevant System.DateTime, etc

Edit: sorting should then work nicely as well

这篇关于不完整日期策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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