我正在使用一个仅包含日期的变量,并尝试以dd/mm/yyyy格式获取DateTime.Now时间,但它只返回mm/dd/yyyy格式的日期。
我严格需要当前日期以dd/mm/yyyy格式显示,但我还没有找到如何实现。
以下是我转换DateTime.Now为DateOnly类型的示例:
public class Example
{
public DateOnly? Date{get; set;}
}
public class Process1
{
Example example = new Example();
{
example.Date= DateOnly.FromDateTime(DateTime.Now);
//this is returning the current date in a mm/dd/yyyy format
}
}
DateOnly
is a binarystruct
, if you want to represent it as astring
use formatting, saystring text = Date.ToString(formatHere);
- Dmitry BychenkoDateTime
和DateOnly
都没有格式。只有当该值被转换为字符串时,格式化才会发挥作用,可以明确地使用.ToString()
进行显式转换,也可以通过多种方式隐式转换(因为您在调试器中查看它,将其提供给Console.WriteLine
等)。 - Jeroen MostertDateTime
和DateOnly
根本没有格式,它们是二进制值。那个答案展示了如何使用特定格式将它们格式化为字符串。这些类型本身存储自0001-01-01以来的滴答或日偏移量。 - Panagiotis Kanavos