Can not convert from DateTime to string

  • Thread starter Thread starter TheTortoise
  • Start date Start date
T

TheTortoise

Guest
Hi I am leaning C# and my program is running however when I go to run the validation test. It's giving me the error code Cannot convert from Date.Time to string. Not sure what I am missing here. Any help you can give to lead me in the right direction is greatly appreciated, thanks.

using System;

namespace Holiday
{
public static class Program
{
public static void Main()
{
Console.Write("Enter the day you are leaving: ");
string leaveDate = Console.ReadLine();

Console.Write("Enter duration: ");
int lengthStay = int.Parse(Console.ReadLine());

Console.WriteLine(DayReturning(leaveDate, lengthStay));
Console.ReadLine();
}

// TODO: Create a method that takes the day you are leaving on vacation and how many days
// you will be gone and return the name of the day of the week you will return.


public static string DayReturning(string day, int daysGone)
{
// convert day of week given to type DayOfWeek
DayOfWeek start = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), day);

// convert day of week to integer (0 is Sunday, 1 is Monday,..., 6 is Saturday )
int startNum = Convert.ToInt32(start);

// calculate the number of the final day by adding days gone to previous integer
int finalDayNum = (startNum + daysGone) % 7;

// convert int value of final day to DayOfWeek type
DayOfWeek end = (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), finalDayNum);


string day1 = end.ToString();

return day1;
}
}
}

Here is the test code

using Holiday;
using System;
using Xunit;

namespace Test
{
public class ProgramTest
{
[Fact]
public void DayReturning_ReturnValue()
{
object result = Program.DayReturning(DateTime.Today, (((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek + 7) % 7) + 1);

Assert.Equal("Tuesday", result.ToString());
}
}
}

Continue reading...
 
Back
Top