c# unit test datetime against string - c#

I am doing a unit test for datetime and it fails because Test Name:
GetDateTime2 Result Message: Assert.AreEqual failed.
Expected:<28/05/2017 20:00:00 (System.String)>. Actual:<28/05/2017
20:00:00 (System.DateTime)>.
Is there any way of comparing the string against the datetime of do i have to change the properties of string date and string time?
public void GetDateTime()
{
FootballEvent football = new FootballEvent();
football.Time = "20:00:00";
football.Date = "28/05/2017";
var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
Assert.AreEqual(expected, footballtime);
}

as some people have stated within the comments why not just make a DateTime object then compare them?
Example:
var footballtime = football.GetDateTime();
var expected = "28/05/2017 20:00:00";
DateTime expectedDate = DateTime.Parse(expected);
Assert.AreEqual(expectedDate, footballtime);

You could call ToString() with whatever format you want your time to be in for comparison.
var expected = "28/05/2017 20:00:00";
//Use HH for 00-23, use H for 0-23, use hh for 01-12 and use h for 1-12
Assert.AreEqual(expected, footballtime.ToString("dd/MM/yyyy HH:mm:ss"));
However it seems like you should be comparing both in the proper format (DateTime).
Ask yourself why you are initializing the times via string (football.Time = "20:00:00";), why not use the proper data type for Dates and Times (DateTime)

Related

How to parse a timespan in order to add it to a datetime?

I've got a string in the following format: 05/06/2019|1330|60
The output I'm looking for is: 05/06/2019T14:30:00
I'm attempting to parse out the TimeSpan portion right now:
public static string getProcedureEndingDateTime (string input) {
//05/06/2019|1330|60
string myDate = input.Split ( '|' ) [0];
DateTime myDateTime = DateTime.Parse (myDate);
string myTime = input.Split('|')[1];
string hours = myTime.Substring(0,2);
string minutes = myTime.Substring(2,2);
TimeSpan myTimeSpan = TimeSpan.Parse($"{hours}:{minutes}");
myDateTime.Add(myTimeSpan);
return myDateTime.ToString();
}
But right now, getting the following output:
To get the above output I'm calling my function like so:
Console.WriteLine (getProcedureEndingDateTime("05/06/2019|1330|60"));
How do I parse the string "1330" into a TimeSpan?
No need to us a Timespan here, just call ParseExact instead with a proper format to do it in one line.
var myDateTime = DateTime.ParseExact("05/06/2019|1330|60", "dd/MM/yyyy|HHmm|60", CultureInfo.InvariantCulture);
Console.WriteLine(myDateTime.ToString());
//this gives 2019-06-05 1:30:00 PM, format depends on your PC's locale
I don't know what the 60 part is, you can adjust the format or substring it out beforehand.
The problem is because Add() returns a new DateTime instance, which means you're currently discarding it. Store it, and return that from your function instead, like so:
var adjusted = myDateTime.Add(myTimeSpan);
return adjusted.ToString();
Try using the numeric values as exactly that, numbers.
Also, the other issue with your code is the DateTime.Add() method doesn't add to that DateTime variable. Instead it returns a new variable, which you are ignoring.
Try this:
public static string getProcedureEndingDateTime (string input) {
string[] parts = input.Split('|');
string myDate = parts[0];
DateTime myDateTime = DateTime.Parse (myDate);
string myTime = parts[1];
if (!int.TryParse(myTime.Substring(0,2), out int hours))
hours = 0;
if (!int.TryParse(myTime.Substring(2,2), out int minutes))
minutes = 0;
TimeSpan myTimeSpan = new TimeSpan(hours, minutes, 0);
myDateTime += myTimeSpan;
return myDateTime.ToString();
}
Assuming the date shown is May 6th (and not June 5th), and also assuming the 60 represents a time zone offset expressed in minutes west of GMT, and also assuming you want the corresponding UTC value, then:
public static string getProcedureEndingDateTime (string input) {
// example input: "05/06/2019|1330|60"
// separate the offset from the rest of the string
string dateTimeString = input.Substring(0, 15);
string offsetString = input.Substring(16);
// parse the DateTime as given, and parse the offset separately, inverting the sign
DateTime dt = DateTime.ParseExact(dateTimeString, "MM/dd/yyyy|HHmm", CultureInfo.InvariantCulture);
TimeSpan offset = TimeSpan.FromMinutes(-int.Parse(offsetString));
// create a DateTimeOffset from these two components
DateTimeOffset dto = new DateTimeOffset(dt, offset);
// Convert to UTC and return a string in the desired format
DateTime utcDateTime = dto.UtcDateTime;
return utcDateTime.ToString("MM/dd/yyyy'T'HH:mm:ss", CultureInfo.InvariantCulture);
}
A few additional points:
Not only is the input format strange, but so is your desired output format. It is strange to see a T separating the date and time and also see the date in the 05/06/2019 format. T almost always means to use ISO 8601, which requires year-month-day ordering and hyphen separators. I'd suggest either dropping the T if you want a locale-specific format, or keep the T and use the standard format. Don't do both.
In ISO 8601, it's also a good idea to append a Z to UTC-based values. For DateTime values, the K specifier should be used for that. In other words, you probably want the last line above to be:
return utcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ssK", CultureInfo.InvariantCulture);
// outputs: "2019-05-06T14:30:00Z"
You might want to not format a string here, but instead return the DateTime or DateTimeOffset value. It's usually better to create a string only at the time of display.
Don't forget that the DateTime struct is immutable. In your question you were ignoring the return value of the Add method.

Why is NUnit Framework throwing DateTime AssertionException?

I'm trying to format a DateTime, but not change the property to a type of string.
My property is as follows DateTime DateApplicationReceived = FormatDateTime();
I have made a small function to do so which allows me to parse my string into a type of DateTime.
private static DateTime FormatDateTime()
{
var format = DateTime.UtcNow.ToString("G");
var dateTime = DateTime.Parse(format);
return dateTime;
}
Let's say the date & time is now: 16/01/2019 15:30:00. I use NUnit to test that my function works by doing the following assert:
public void ConvertsTime()
{
var sut = DateApplicationReceived;
Assert.That(sut, Is.EqualTo("16/01/2019 15:30:00"));
}
My test fails. The expected result ignores the format (of "G") and displays it to a different culture 2019-01/16.
Debug info:
Why does it do this? I'm explicity giving it a format & changing the culture variant doesn't seem to have any effect.
The DateTime object has no format.
So your code here is useless:
var format = DateTime.UtcNow.ToString("G");
var dateTime = DateTime.Parse(format);
This is fully equivalent to
var dateTime = DateTime.UtcNow;
In your unit test, you should compare either a DateTime with a DateTime, or a string with a string. Currently, you are comparing a DateTime with a string.
So you should change your code to:
Assert.That(sut.ToString("G"), Is.EqualTo("16/01/2019 15:30:00"));
or
Assert.That(sut, Is.EqualTo(DateTime.Parse("16/01/2019 15:30:00")));

How to get the desired output by modifying string after removing / from string

I have a string like this:
30/04/2018 o/p=300418
01/03/2017 o/p=010317
10/11/2018 o/p=101118
12/11/2123 o/p=121123
1/1/2018 o/p =010118
code tried but can't get the last one 1/1/2018
string a = "31/04/2018";
string b = a.Replace("/","");
b = b.Remove(4, 2);
You should parse to a DateTime and then use the ToString to go back to a string. The following works with your given input.
var dateStrings = new []{"30/04/2018", "01/03/2017","10/11/2018","12/11/2123","1/1/2018"};
foreach(var ds in dateStrings)
{
Console.WriteLine(DateTime.ParseExact(ds, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("ddMMyy"));
}
The only change I made is to the first date as that is not a valid date within that month (April has 30 days, not 31). If that is going to be a problem then you should change it to TryParse instead, currently I assumed your example was faulty and not your actual data.
Your structure varies, all of the examples above use two digit month and day, while the bottom only uses a single digit month and day. Your current code basically will replace the slash with an empty string, but when you remove index four to two your output would deviate.
The simplest approach would be:
var date = DateTime.Parse("...");
var filter = $"o/p = {date:MMddyyyy}";
Obviously you may have to validate and ensure accuracy of your date conversion, but I don't know how your applications works.
If you can reasonably expect that the passed in dates are actual dates (hint: there are only 30 days in April) you should make a function that parses the string into DateTimes, then uses string formats to get the output how you want:
public static string ToDateTimeFormat(string input)
{
DateTime output;
if(DateTime.TryParse(input, out output))
{
return output.ToString("MMddyy");
}
return input; //parse fails, return original input
}
My example will still take "bad" dates, but it will not throw an exception like some of the other answers given here (TryParse() vs Parse()).
There is obviously a small bit of overhead with parsing but its negligible compared to all the logic you would need to get the proper string manipulation.
Fiddle here
Parse the string as DateTime. Then run ToString with the format you desire.
var a = "1/1/2018";
var date = DateTime.Parse(a);
var result = date.ToString("ddMMyyyy");
You can use ParseExact to parse the input, then use ToString to format the output.
For example:
private static void Main()
{
var testData = new List<string>
{
"31/04/2018",
"01/03/2017",
"10/11/2018",
"12/11/2123",
"1/1/2018",
};
foreach (var data in testData)
{
Console.WriteLine(DateTime.ParseExact(data, "d/m/yyyy", null).ToString("ddmmyy"));
}
GetKeyFromUser("\nDone! Press any key to exit...");
}
Output
You didn't specify whether these are DateTime values or just strings that look like date time values. I'll assume these are DateTime values.
Convert the string to a DateTime. Then use a string formatter. It's important to specify the culture. In this case dd/mm/yyyy is common in the UK.
var culture = new CultureInfo("en-GB");//UK uses the datetime format dd/MM/yyyy
var dates = new List<string>{"30/04/2018", "01/03/2017","10/11/2018","12/11/2123","1/1/2018"};
foreach (var date in dates)
{
//TODO: Do something with these values
DateTime.Parse(date, culture).ToString("ddMMyyyy");
}
Otherwise, running DateTime.Parse on a machine with a different culture could result in a FormatException. Parsing dates and times in .NET.

C#: DateTime problems

In a variable of DateTime typeI have this value = {30/07/2014 0:00:00}
I want only the date:
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
with it I obtain 30/04/2014 correctly
but when I want to convert in MM/dd/yyyy using:
var aux2 = DateTime.ParseExact(aux, "MM/dd/yyyy", null);
I have this error:
the string is represents one DateTime not admited in the GregorianCalendar
Why I have this error in aux2?
The problem is your locale setting. Calling ToString() without parameters on a date value produces a string with the pattern day,month,year arranged differently between locales. (And I suppose that you get a string arranged with Day,Separator, Month, Separator, Year).
Passing that string to DateTime.ParseExact with a specific pattern (MM/dd/yyyy) requires the string to be in the exact pattern required Month, Day, Year for your example.
You could force the invariant culture in your conversion with
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
this produces a string with the pattern required by the subsequent ParseExact mask
However it is not clear why you need these conversions. A date is not a string and you simply keep it as a date and use the conversion only when you need to represent it somewhere (display, print etc...)
Console.WriteLine("Date is:" + pedido.ord_cus_deliv_date.ToString("MM/dd/yyyy"));
When you call below :
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
This gives you code "07-30-2014" and not "07/30/2014" and that's generate the error while conversion. So to get "07/30/2014", you have to write
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
Below is overall code for you:
DateTime value = DateTime.Parse("30/07/2014 0:00:00"); //your date time value
var aux = value.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
DateTime dt = DateTime.ParseExact(aux, "MM/dd/yyyy", CultureInfo.InvariantCulture);
var aux2 = dt.ToString(CultureInfo.InvariantCulture).Split(' ')[0]);
I hope this will help you
Regards,
Sandeep

Datetime value with different culture not formatting correctly

I'm having a slight issue with Thread culture and getting a date to display properly.
I am overloading the ToString() method of the DateTime class.
With culture "en-CA", my date is coming out in the right format "yyyy/MM/dd"
but with culture "fr-CA", my date is coming out "yyyy-MM-dd"
I've made some unit test to display the issue.
The english test works but the french always fails.
Even if I change the GetDateInStringMethod to do .ToShortDateString. I still get the same issue.
[Test()]
public void ValidInEnglish()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = Utility.DatePattern;
DateTime? currentDate = new DateTime(2009,02,7);
string expected = "2009/02/07";
string actual = DateUtils.GetDateInString(currentDate);
//This works
Assert.AreEqual(expected, actual);
}
[Test()]
public void ValidInFrench()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = Utility.DatePattern;
DateTime? currentDate = new DateTime(2009, 02, 7);
string expected = "2009/02/07";
string actual = DateUtils.GetDateInString(currentDate);
// This doesn't work
Assert.AreEqual(expected, actual);
}
public static string GetDateInString(DateTime? obj)
{
if (obj == null || !obj.HasValue)
{
return string.Empty;
}
return obj.Value.ToString(Utility.DatePattern);
}
public const string DatePattern = "yyyy/MM/dd";
Change this line:
return obj.Value.ToString(Utility.DatePattern);
to this:
return obj.Value.ToString(Utility.DatePattern, CultureInfo.InvariantCulture);
Read about it here: System.Globalization.InvariantCulture
That doesn't work because using french culture defaults the datetime formatter to use - instead of / as a separator character. If you want to keep your date the same no matter the culture then use CultureInfo.InvariantCulture if you want to use the french formatting the change your expected test result to "2009-02-07". If you are looking for more info check this msdn link.
And if you want a personal recommendation for a lib to use for dealing with the awesomeness that is Globalization then I'd recommend Noda Time.

Categories

Resources