格式化日期时间而不将其转换为 c# 中的字符串?



在我的应用程序中,我需要有一个具有这种格式的DateTime变量:"dd.MM.yyyy HH:mm:ss",但它不应该是一个字符串。

这些是我到目前为止尝试过的事情:

var now = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
var currentTime = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var currentTime1 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", CultureInfo.CurrentCulture);
var turkishCultureInfo = CultureInfo.CreateSpecificCulture("tr-TR");
var currentTime2 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", turkishCultureInfo);
var currentTime3 = DateTime.ParseExact(now, "dd.MM.yyyy HH:mm:ss", null);
Debug.Log("now: " + now); // 06.01.2023 21:25:27
Debug.Log("currentTime: " + currentTime); // 1/6/2023 9:25:27 PM
Debug.Log("currentTime1: " + currentTime1); // 1/6/2023 9:25:27 PM
Debug.Log("currentTime2: " + currentTime2); // 1/6/2023 9:25:27 PM
Debug.Log("currentTime3: " + currentTime3); // 1/6/2023 9:25:27 PM

正如你在上面看到的,我尝试了ParseExact 方法与 CultureInfo.InvariantCulture、CultureInfo.CurrentCulture、turkishCultureInfo 和 null 都有相同的输出。 2023/1/6 21:25:27 PM,但我需要这样 06.01.2023 21:25:27

我不想使用字符串,因为我需要使用减法、AddSeconds 等方法,这就是为什么我需要 DateTime 类型变量的原因。有没有办法将"DateTime.Now"格式化为"dd.MM.yyyy HH:mm:ss"?


上面的例子不是我真正的问题。我只是想展示我需要什么,但我相信它不是那么清楚。这是我的实际问题:

var now = DateTime.Now;
TimeSpan totalTime = now.Subtract(DateTime.ParseExact(lastTime, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture));
">

lastTime"是从数据库中检索的字符串值,格式为"dd.MM.yyyy HH:mm:ss">

但是当我尝试在"现在"和"上次"之间获取TimeSpan时,我收到此错误:

FormatException: String was not recognized as a valid DateTime.
System.DateTimeParse.ParseExact (System.ReadOnlySpan`1[T] s, System.ReadOnlySpan`1[T] format, System.Globalization.DateTimeFormatInfo dtfi, System.Globalization.DateTimeStyles style) (at <d6232873609549b8a045fa15811a5bd3>:0)
System.DateTime.ParseExact (System.String s, System.String format, System.IFormatProvider provider) (at <d6232873609549b8a045fa15811a5bd3>:0)

我认为这里有点混乱字符串连接对您的值的作用。

它无非是简单地使用默认格式进行无参数ToString()

你的每个字符串都喜欢

Debug.Log("currentTime: " + currentTime);

基本内部隐式相等

Debug.Log("currentTime: " + currentTime.ToString());

在这种情况下,可以省略不带参数的ToString(),因为 c# 已经隐式使用它。

=> 您在所有示例中都使用了string!您认为如何(作为字符串!)将任何内容记录到控制台?;)

正如评论中所指出的,DataTime只是一个时间点,基本上只是一个围绕long的包装器,这是自Unix时代以来通过的系统滴答声。

所有其他属性、值和格式都是根据这些按需报价计算的(顺便说一句,这使得它非常昂贵)。

有没有办法将"DateTime.Now"格式化为"dd.MM.yyyy HH:mm:ss">

是的,正是你是如何做到的。

所以你想做的只是使用DateTime进行所有的计算和AddMinutes等,然后只有当你需要显示(当前)值时,你才简单地以正确的格式传递,就像你在第一行所做的那样

const string format = "dd.MM.yyyy HH:mm:ss";

Debug.Log("currentTime: " + currentTime.ToString(format));
<小时 />

更新

现在终于知道您的实际代码和问题

这里没有理由使用解析...你想要的是

TimeSpan totalTime = DateTime.Now - lastTime;

然后Debug.Log(totalTime.ToString(YOUR_DESIRED_FORMAT));

您可以创建DateTimeExtensions类,以轻松获取预期形式的字符串:

public static class DateTimeExtensions
{
const string myFormat = @"dd.MM.yyyy HH:mm:ss";
public static string GetInMyFormat(this DateTime dt)
=> dt.ToString(myFormat);
}

并使用它:

var myDateTime = DateTime.Now.AddHours(-36.23);
Debug.Log("myDateTime in my format: " + DateTime.Now.GetInMyFormat());

最新更新