what datatype to use to store "MMYYYY" format
I am using .NET environment and wanted to track the expiration date found in credit cards. The date there is of the nature "MMYYYY". What would be a recommended way to store this ? Should I just use .NET string or instead use 开发者_开发问答the .NET DateTime object ?
In almost all cases the easiest solution is often the best solution. Its easy to maintain, easy for other programmers to recognize, and may even keep you out of trouble (ints and strings can lead to localization problems and invalid values).
A System.DateTime
is the best option and gives you flexibility for those cards that may also expire on a given day (they are rare, but they do exist, and may become more prevalent with temporary credit cards becoming popular). When storing the value just use the Date
property of the System.DateTime
struct since that will disregard the time components.
Store MM and YYYY as two separate integer values.
for this case that you want to store expiration of credit card, it's better if you define your struct like this. this ensures that invalid values will not happen.
public enum MonthOfYear
{
Jan = 1,
Feb = 2,
...
}
public struct CardExpiration
{
public MonthOfYear Month;
public int Year;
}
although, you can use DateTime to make it quicker.
Another way to store it as integer where result is:
VALUE = YYYY * 100 + MM
So for Jul 2011 you will have 2011 * 100 + 07 = 201107 for Oct 2015 you will have 201510 and Dec 2014 your will have 201412. This way to do comparison if card is expired or not very easy, since you just need to compare two numbers.
精彩评论