I'm trying to make a C# method to fulfill this user story.
These are the 2 acceptance criteria
Start time must be at least one hour later than the current system time.
End time must be at last one hour after start time.
Both of the start and end time must be DateTime values, so I can parse it with the TryParse method.
Here's what I have in my code so far:
`
private DateTime datetime;
public DateTime datetimeStart { get; set; }
public DateTime datetimeEnd { get; set; }
while (true) {
Util.GetInput("Delivery window start (dd/mm/yyyy hh:mm)");
string userInput = ReadLine();
if(DateTime.TryParse(userInput, out datetime))
{
if (datetime.TimeOfDay.Hours - DateTime.Now.TimeOfDay.Hours >= 1) {
datetimeStart = datetime;
}
break;
}
else
{
WriteLine("\tDelivery window start must be at least one hour in the future.");
}
}
while (true) {
Util.GetInput("Delivery window end (dd/mm/yyyy hh:mm)");
string userInput = ReadLine();
if(DateTime.TryParse(userInput, out datetime))
{
if (datetime.TimeOfDay.Hours - datetimeStart.TimeOfDay.Hours >= 1) {
datetimeEnd = datetime;
}
break;
}
else
{
WriteLine("\tDelivery window end must be at least one hour later than the start.");
}
}
`
I'm not fully sure how the DateTime type works yet, but later on, I'd need to get an output string with this format:
"The pickup window for your order will be 04:00 on 30/10/2022 and 20:00 on 30/10/2022", and just replace the data in the string with values from datetimeStart and datetimeEnd
DateTime provides all the tools to write your conditions in straightforward code:
one hour later than / after checkTime
is just
checkTime.AddHours(1)
and
someTime must be at least one hour later than / after checkTime
becomes
someTime >= checkTime.AddHours(1)
So your code may look something like this:
...........................
if (datetime >= DateTime.Now.AddHours(1)) {
datetimeStart = datetime;
}
...........................
if (datetime >= datetimeStart.AddHours(1)) {
datetimeEnd = datetime;
}
...........................
A general rule of thumb is that any internal time keeping should be done in UTC, but when presenting in a UI (form or console) that you may show in local time.
Another rule is that when comparing DateTime objects that they should have the same Kind.
Perhaps add something like:
DateTime earliestStartTime = DateTime.UtcNow.AddHours(1);
Later if you have a variable named startTime you want to make sure that it is greater than or equal to earliestStartTime. Once you have startTime set, you can then have:
DateTime earliestEndTime = startTime.AddHours(1);
and likewise compare endTime to earliestStartTime for validity.
When presenting times to the user, you can use the .ToLocalTime() method.
'Following is the code, I want to be check stored in database date and time if greater or equal then current date and time, if current date is greater then stored date then how to compare Time in this check
DateTime sysDate = DateTime.Now.Date;
int sysMinute = DateTime.Now.Minute;
int sysHour = DateTime.Now.Hour;
DateTime scheduledDate = Convert.ToDateTime(item.Scheduled_Start_Date.ToString()).Date;
int scheduledMinute = Convert.ToDateTime(item.Scheduled_Start_Time.ToString()).Minute;
int scheduledHour = Convert.ToDateTime(item.Scheduled_Start_Time.ToString()).Hour;
if (sysDate >= scheduledDate && sysMinute >= scheduledMinute && sysHour >= scheduledHour)
{
var currency = GetEventCurrency(evDB.EventID);
ev.PriceRange = currency.GetFormattedAmount(item.ScheduledPrice.ToString());
isScheduledPrice = true;
}
When getting system datetime with DateTime.Now you have to be aware that several calls may produce incoherent results. This is specially important on the change of the minute, hour or even day.
Do just one call and operate with that result.
In your question is not necessary to segregate DateTime components.
DateTime sysDate = DateTime.Now;
DateTime scheduledDate = Convert.ToDateTime(item.Scheduled_Start_Date.ToString()).Date;
DateTime scheduledTime = Convert.ToDateTime(item.Scheduled_Start_Time.ToString()).Time;
DateTime scheduledDateTime = scheduledDate + scheduledTime;
Once scheduled datetime is in C# format you can just compare them:
if (sysDate >= scheduledDateTime)
{
var currency = GetEventCurrency(evDB.EventID);
ev.PriceRange = currency.GetFormattedAmount(item.ScheduledPrice.ToString());
isScheduledPrice = true;
}
Datetimes in C# are stored in ticks (100 ns) from 00:00:00 of January 1st of year 1 up to the expressed date and time.
Comparison thus is easy as those are just scalars (internally).
I am developping an application where a user can download various reports. There is one report per month and each report is called "YYYY-MM.txt". An user can only download files of the last 18 months.
I have written a function that takes in parameter a list of filespath and then downloading them to the client. My problem is how to add files in this list, basically how can I check if a file is in the last 18 months, knowing that I have his year and month, and the current year and month.
This is what I have :
//just for test, supposed that theses values were extracted from the report of august 2014.
string fileYear = "2014";
string fileMonth = "08";
string currentYear = DateTime.Now.Year.ToString();
string currentMonth = DateTime.Now.Month.ToString();
How can I compare fileYear and fileMonth with currentYear and currentMonth to know if the report correspond to a month of the last 18.
Thanks in advance for your help
Here's how I would do it.
int fileYear = int.Parse(fileName.Substring(0,4));
int fileMonth = int.Parse(fileName.Substring(5,2));
DateTime oldestDate = DateTime.Now.AddMonths(-18);
int oldestYear = oldestDate.Year;
int oldestMonth = oldestDate.Month;
if(fileYear > oldestYear || (fileYear == oldestYear && fileMonth >= oldestMonth))
{
// This file is within 18 months.
}
This means that if today is 12-31-2014 it will include files back to 2013-06.txt. If needed you can also put an upper bounds check in case you could have files with future dates.
EDIT
The other alternative is to create a DateTime from the file name to compare. Here's how I would do that to ensure I'm comparing the last day of the file's month
int fileYear = int.Parse(fileName.Substring(0,4));
int fileMonth = int.Parse(fileName.Substring(5,2));
DateTime fileDate = new DateTime(fileYear, fileMonth, 1).AddMonths(1).AddDays(-1);
DateTime oldestDate = DateTime.Now.AddMonths(-18);
if(fileDate.Date >= oldestDate.Date)
{
// This file is within 18 months.
}
You could do something like this:
https://dotnetfiddle.net/VORvZr
using System;
public class Program
{
public static void Main()
{
DateTime fileDate = new DateTime(2013, 5, 1);
DateTime fileDateNewer = new DateTime(2014, 1, 1);
GetMonthDifference(fileDate);
GetMonthDifference(fileDateNewer);
}
public static void GetMonthDifference(DateTime fileDate)
{
DateTime currentDate = DateTime.Now;
DateTime eighteenMonthsAgo = currentDate.AddMonths(-18);
if (eighteenMonthsAgo > fileDate)
Console.WriteLine("{0} is greater than or equal to 18 months ago", fileDate);
else
Console.WriteLine("{0} is less than 18 months ago", fileDate);
}
}
Note that if you can, you always want to try to work with objects that most closely represent your data. E.g. if working with years you should work with a numeric type rather than string type. In this case, working with dates.
EDIT:
as comments posted on the other answers pointed out, you would have some room for error depending on the day the file was uploaded/created if it's right around the 18 month mark. Something you could potentially do is get the actual file creation date (assuming you are the system creating the file and the date of the file creation coincides with the month the data belongs. You can get a files creation date as such:
string fullFilePathAndName = #""; // wherever your file is located
FileInfo fi = new FileInfo(fullFilePathAndName);
DateTime fileCreateDate = fi.CreationTime
I am creating a function that will set the date of an event, based on the current time.
I have an enumeration of events:
public enum EventTimings
{
Every12Hours, // 12pm and midnight
Weekly // sunday at midnight
}
public static DateTime CalculateEventTime(EventTimings eventTime)
{
DateTime time;
switch(eventTime)
{
case EventTimings.Every12Hours:
break;
}
return time;
}
So (Every12Hour event type) if the current time is 10am, then the eventdate will be the same day but at 12pm.
How should I write this?
I also have to make sure this works for December 31st and any other strange outlier date/time.
Is datetime the best for this scenerio?
If you want to be able to test anything, I would make the DateTime you are trying to "round" explicit, something like
public static DateTime RoundedDate(DateTime eventTime, EventTimings strategy)
{
switch (strategy)
case EventTimings.Weekly :
return WeeklyRounding(eventTime);
... etc ...
That way you can now write a specialized method for the 12-hour interval, the week interval, and test it for any input date possible, without depending on your computer clock.
You could also try something like this, although it breaks down if you want to do something monthly (because months each have a different number of days.) Also, while this simplified method will ensure a returned date at 12 and midnight, the weekly offset would be every 7 days from the starting day... not necessarily on Sundays. You could easily accomodate that behavior with a switch statement, though. The overloaded method also allows you some flexibility to provide a custom offset.
Also, to answer your question, yes I would use System.DateTime and System.TimeSpan. They handle determining whether a year or month has "rolled over" for you.
public enum EventTimings : int
{
Default = 12, // Default every 12 hours.
NoonAndMidnight = 12, // Every 12 hours.
Weekly = 168, // 168 hours in a week.
ThirtyDays = 720 // 720 hours in 30 days.
}
public DateTime CalculateDateTime(DateTime starting, EventTimings timing)
{
return CalculateDateTime(starting, TimeSpan.FromHours((int)timing));
}
public DateTime CalculateDateTime(DateTime starting, TimeSpan span)
{
DateTime baseTime = new DateTime(starting.Year, starting.Month, starting.Day, starting.Hour >= 12 ? 12 : 0, 0, 0);
return baseTime.Add(span);
}
I agree to keep it generic by making the reference date an input parameter instead of current datetime. However as you have asked about the logic for your eventTime values as well, this is how I would go about.
How should I write this?
For every12hours, check the hour property of the input date and check if it is less than 12. If so, then create a new TimeSpan for 12pm and add it to the datepart of the input date.
If not, add 1 day to the input date, create a TimeSpan for 12am and add it to the datepart of inputdate.
For weekly (Monday 12am), check the dayoftheweek of the inputdate and just add number of days to make it equal to the incoming Monday (Which is as simple as (8 - dayoftheweek)) and add a 12am TimeSpan to the date of the incoming Monday's date.
public enum EventTimings
{
Every12Hours, // 12pm and midnight
Weekly // sunday at midnight
}
public static DateTime CalculateEventTime(EventTimings eventTime, DateTime inputDate)
{
DateTime time = DateTime.Now;
switch (eventTime)
{
case EventTimings.Every12Hours:
time = inputDate.Hour > 12 ? inputDate.AddDays(1).Date + new TimeSpan(0, 0, 0) : inputDate.Date + new TimeSpan(12, 0, 0);
return time;
case EventTimings.Weekly:
int dayoftheweek = (int) inputDate.DayOfWeek;
time = inputDate.AddDays(8 - dayoftheweek).Date + new TimeSpan(0, 0, 0);
return time;
// other cases
}
}
Is datetime the best for this scenerio?
Yes. Your datetime calculations using DateTime and TimeSpan should take care of leap years, daylight savings or endofyear scenarios. Additionally you could try adding SpecifyKind to denote it is local time.
The algorithm I'd follow goes like this...
Put noon on the day of eventTime into a variable
Check if that variable is before eventTime
If it's not, add 12 hours to it
Return the variable
switch (strategy)
{
case EventTimings.Every12Hours:
//get noon for the event date
DateTime x = eventTime.Date.AddHours(12);
//check to see if x is after the eventTime
if (x < eventTime)
{
//if so, advance x by 12 hours to get midnight on the next day
x = x.AddHours(12);
}
return x;
break;
//other cases...
}
This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do
//DOB's format is "MMM (D)D YY" that first digit of the day is not there for numbers 1-9
string tmpYear = rowIn.DOB.Substring(rowIn.DOB.Length - 3, 2); //-3 because it is 0 indexed
if (Convert.ToInt16(tmpYear) > 50)
tmpYear = String.Format("19{0}", tmpYear);
else
tmpYear = String.Format("20{0}", tmpYear);
I am sure I am doing it horribly wrong, any pointers?
The .NET framework has a method that does exactly what you want:
int fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(twoDigitYear)
That way you will correctly adhere to current regional settings as defined in Control Panel (or group policy):
Given that there are people alive now born before 1950, but none born after 2010, your use of 50 as the flipping point seems broken.
For date of birth, can you not set the flip point to the 'year of now' (i.e. 10) in your app? Even then you'll have problems with those born before 1911...
There's no perfect way to do this - you're creating information out of thin air.
I've assumed DOB = date-of-birth. For other data (say, maturity of a financial instrument) the choice might be different, but just as imperfect.
You can also use the DateTime.TryParse method to convert your date. It uses the current culture settings to define the pivot year (in my case it is 2029)
DateTime resultDate;
Console.WriteLine("CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : {0}", System.Globalization.CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax);
DateTime.TryParse("01/01/28", out resultDate);
Console.WriteLine("Generated date with year=28 - {0}",resultDate);
DateTime.TryParse("01/02/29",out resultDate);
Console.WriteLine("Generated date with year=29 - {0}", resultDate);
DateTime.TryParse("01/03/30", out resultDate);
Console.WriteLine("Generated date with year=30 - {0}", resultDate);
The output is:
CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : 2029
Generated date with year=28 - 01/01/2028 00:00:00
Generated date with year=29 - 01/02/2029 00:00:00
Generated date with year=30 - 01/03/1930 00:00:00
If you want to change the behavior you can create a culture with the year you want to use as pivot. This thread shows an example
DateTime.TryParse century control C#
But as martin stated, if you want to manage a time period that spans more than 100 year, there is no way to do it with only 2 digits.
I think Java has a good implementation of this:
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#year
People rarely specify years far into the future using a two-digit code. The Java implementation handles this by assuming a range of 80 years behind and 20 years ahead of the current year. So right now, 30 would be 2030, while 31 would be 1931. Additionally, this implementation is flexible, modifying its ranges as time goes on, so that you don't have to change the code every decade or so.
I just tested, and Excel also uses these same rules for 2-digit year conversion. 1/1/29 turns into 1/1/2029. 1/1/30 turns into 1/1/1930.
The implementation of
System.Globalization.CultureInfo.CurrentCulture.Calendar.ToFourDigitYear
is
public virtual int ToFourDigitYear(int year)
{
if (year < 0)
throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (year < 100)
return (this.TwoDigitYearMax / 100 - (year > this.TwoDigitYearMax % 100 ? 1 : 0)) * 100 + year;
else
return year;
}
Hope this helps!
It might be smarter to check tmpYear > currentYear%100. If it is, then it's 19XX, otherwise 20XX.
This solution we use for Expiration Dates, the user enters MM and YY into separate fields. This results in dates being the 31st or 30th and 28th or 29th also for February.
/// <summary>
/// Creates datetime for current century and sets days to end of month
/// </summary>
/// <param name="MM"></param>
/// <param name="YY"></param>
/// <returns></returns>
public static DateTime GetEndOfMonth(string MM, string YY)
{
// YY -> YYYY #RipVanWinkle
// Gets Current century and adds YY to it.
// Minus 5 to allow dates that may be expired to be entered.
// eg. today is 2017, 12 = 2012 and 11 = 2111
int currentYear = DateTime.Now.Year;
string thisYear = currentYear.ToString().Substring(0, 2) + YY;
int month = Int32.Parse(MM);
int year = Int32.Parse(thisYear);
if ((currentYear - 5) > year)
year += 100;
return new DateTime(year, month, DateTime.DaysInMonth(year, month));
}
This Method can convert the credit card last two year digits to four year
private static int ToFourDigitYear(int year)
{
string stringYear = year.ToString("00");
if (stringYear.Length == 2)
{
int currentYear = DateTime.Now.Year;
string firstTwoDigitsOfCurrentYear = currentYear.ToString().Substring(0, 2);
year = Convert.ToInt32(firstTwoDigitsOfCurrentYear + stringYear);
if (year < currentYear)
year = year + 100;
}
return year;
}
Out of curiosity, from where do you get this data? From a form? In that case; I would simply ask the user to fill in (or somehow select) the year with four digits or get the users age and month/day of birth, and use that data to figure out what year they were born. That way, you wouldn't have to worry about this problem at all :)
Edit: Use DateTime for working with this kind of data.
Try this simple code
//Invoke TextBoxDateFormat method with date as parameter.
Method
public void TextBoxDateFormat(string str1)
{
// Takes the current date format if MM/DD/YY or MM/DD/YYYY
DateTime dt = Convert.ToDateTime(str1);
//Converts the requested date into MM/DD/YYYY and assign it to textbox field
TextBox = String.Format("{0:MM/dd/yyyy}", dt.ToShortDateString());
//include your validation code if required
}
Had a similar issue, and came up with this... HTH!
value = this.GetDate()
if (value.Length >= 6)//ensure that the date is mmddyy
{
int year = 0;
if (int.TryParse(value.Substring(4, 2), out year))
{
int pastMillenium = int.Parse(DateTime.Now.ToString("yyyy").Substring(0, 2)) - 1;
if (year > int.Parse(DateTime.Now.ToString("yy")))//if its a future year it's most likely 19XX
{
value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium, year.ToString().PadLeft(2, '0'));
}
else
{
value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium + 1, year.ToString().PadLeft(2, '0'));
}
}
else
{
value = string.Empty;
}
}
else
{
value = string.Empty;
}
My answer will not match your question but for credit cards I just add 2 digits of current year
private int UpconvertTwoDigitYearToFour(int yearTwoOrFour)
{
try
{
if (yearTwoOrFour.ToString().Length <= 2)
{
DateTime yearOnly = DateTime.ParseExact(yearTwoOrFour.ToString("D2"), "yy", null);
return yearOnly.Year;
}
}
catch
{
}
return yearTwoOrFour;
}
If you calculate for a person he will probably not be more than 100 years...
Eg: 751212
var nr = "751212";
var century = DateTime.Now.AddYears(-100).Year.ToString().Substring(0, 2);
var days = (DateTime.Now - DateTime.Parse(century + nr)).Days;
decimal years = days / 365.25m;
if(years>=99)
century = DateTime.Now.Year.ToString().Substring(0, 2);
var fullnr = century+nr;
To change a 2-digit year to 4-digit current or earlier -
year = year + (DateTime.Today.Year - DateTime.Today.Year%100);
if (year > DateTime.Today.Year)
year = year - 100;
My two cents,
Given an age range=[18, 100+], two digits year=90, I can do
current year - twoDigitsYear = 2018 - 90 = 1928, I got 19, 28
hence 19 is the first two digits of year of born, and 28 is the age, which is
year=1990, age=28
But it won't work when age 0 and 100 both included in the range, same to some of the other answers here.
Based on above solutions, here is mine, i used in android while using java
it takes current year in two digit format then checks for if input
year length is equal to 2, if yes then it get current year and from
this year it splits first two digits of century, then it adds this
century with year user input. to make it 4 digit year.
public static int getConvertedYearFromTwoToFourDigits(String year) {
if (year.length() == 2) {
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String firstTwoDigitsOfCurrentYear = String.valueOf(currentYear).substring(0, 2);
year = firstTwoDigitsOfCurrentYear + year;
}
return Integer.parseInt(year);
}
int fYear = Convert.ToInt32(txtYear.Value.ToString().Substring(2, 2));
My answer will not match your question but for credit cards I just add 2 digits of current year
private int UpconvertTwoDigitYearToFour(int yearTwoOrFour)
{
try
{
if (yearTwoOrFour.ToString().Length <= 2)
{
DateTime yearOnly = DateTime.ParseExact(yearTwoOrFour.ToString("D2"), "yy", null);
return yearOnly.Year;
}
}
catch
{
}
return yearTwoOrFour;
}