本文介绍了无法从DateTime转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我倾向于使用C#而我的程序正在运行但是当我去运行验证测试时。它给了我错误代码无法从Date.Time转换为字符串。不知道我在这里缺少什么。你可以给我带来正确指导的任何帮助都是
非常感谢,谢谢。 

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;
        }
    }
}

这是测试代码

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());
        }
    }
}






推荐答案

这是使用Holiday的测试代码

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());
        }
    }
}





//object result = Program.DayReturning(DateTime.Today, (((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek + 7) % 7) + 1);

object result = Program.DayReturning(DateTime.Today.ToString(), (((int)DayOfWeek.Monday - (int)DateTime.Today.DayOfWeek + 7) % 7) + 1);




- Wayne


- Wayne


这篇关于无法从DateTime转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 21:39