gpt4 book ai didi

c# - 将 overdueDate 从 DateTime 转换为格式为 dd/mm/yyyy 00 :00:00 的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:08 24 4
gpt4 key购买 nike

我如何将 overdueDate 值从 DateTime 变量转换为 dd/mm/yyyy 00:00:00 ?

    DateTime overdueDate;
DateTime today = DateTime.Now;

if (today.DayOfWeek == DayOfWeek.Monday)
{
overdueDate = today.AddDays(-12);
}
else if (today.DayOfWeek == DayOfWeek.Tuesday)
{
overdueDate = today.AddDays(-13);
}
else if (today.DayOfWeek == DayOfWeek.Wednesday)
{
overdueDate = today.AddDays(-7);
}
else if (today.DayOfWeek == DayOfWeek.Thursday)
{
overdueDate = today.AddDays(-8);
}
else if (today.DayOfWeek == DayOfWeek.Friday)
{
overdueDate = today.AddDays(-9);
}
else if (today.DayOfWeek == DayOfWeek.Saturday)
{
overdueDate = today.AddDays(-10);
}
else if (today.DayOfWeek == DayOfWeek.Sunday)
{
overdueDate = today.AddDays(-11);
}

最佳答案

由于您只需要将时间设置为 00:00:00Date 部分,您可以使用 DateTime.Date属性并将其格式化为:

string str = overdueDate.Date.ToString("dd/MM/yyy HH:mm:ss");

参见:Custom Date and Time Format Strings

你在声明的时候还没有初始化你的overdueDate,虽然你在你的条件下给它赋了一些值,但是编译器不能确定是否有任何值会被赋给它。因此你会得到错误“使用未分配的局部变量”。只需声明如下:

DateTime overdueDate = default(DateTime);

关于c# - 将 overdueDate 从 DateTime 转换为格式为 dd/mm/yyyy 00 :00:00 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com