C# 将字符串转换为日期时间
在大多数情况下,我们以字符串形式获取日期,并且希望分别使用日,月和年。不用担心,在 C# 中,为了将字符串转换为 DateTime
对象,我们使用了一个名为 DateTime
的预定义类。
有几种在 C# 中将字符串转换为 DateTime
的方法,但是在这里,我们仅通过运行示例详细说明三种方法。这些方法是 Convert.ToDateTime()
,DateTime.Parse()
和 DateTime.ParseExact()
。
C# 使用 Convert.ToDateTime()
将字符串转换为 DateTime
Convert.ToDateTime()
的正确语法是
Convert.ToDateTime(dateTobeConverted);
Convert.ToDateTime(dateTobeConverted, cultureInfo);
这里应该注意,如果你不提供区域性信息,那么默认情况下,编译器会将我们的日期字符串视为月/日/年。CultureInfo
是 System.Globalization
命名空间中的一个 C# 类,有关特定的 CultureInfo
。
using System;
public class Conversion {
public static void Main() {
string CurrentDate = "06/04/2020";
// Use of Convert.ToDateTime()
DateTime DateObject = Convert.ToDateTime(CurrentDate);
Console.WriteLine("The Date is: " + DateObject.Day + " " + DateObject.Month + " " +
DateObject.Year);
}
}
输出:
The Date is: 4 6 2020
现在,我们将通过传递 CultureInfo
对象作为参数来实现它。
using System;
using System.Globalization;
public class Conversion {
public static void Main() {
string CurrentDate = "06/04/2020";
// Creating new CultureInfo Object
// You can use different cultures like French, Spanish etc.
CultureInfo Culture = new CultureInfo("en-US");
// Use of Convert.ToDateTime()
DateTime DateObject = Convert.ToDateTime(CurrentDate, Culture);
Console.WriteLine("The Date is: " + DateObject.Day + " " + DateObject.Month + " " +
DateObject.Year);
}
}
输出:
The Date is: 4 6 2020
如果我们将 CultureInfo
更改为 nl-NL
,则将交换月份和日期。
using System;
using System.Globalization;
public class Conversion {
public static void Main() {
string CurrentDate = "06/04/2020";
CultureInfo Culture = new CultureInfo("nl-nl");
DateTime DateObject = Convert.ToDateTime(CurrentDate, Culture);
Console.WriteLine("The Date is: " + DateObject.Day + " " + DateObject.Month + " " +
DateObject.Year);
}
}
输出:
The Date is: 6 4 2020
C# 使用 DateTime.Parse()
将字符串转换为 DateTime
DateTime.Parse()
的语法是,
DateTime.Parse(dateTobeConverted);
DateTime.Parse(dateTobeConverted, cultureInfo);
DateTime.Parse()
方法也是如此,如果我们不提供文化信息作为参数,那么默认情况下,我们的系统会将其视为月/日/年。
如果要转换的字符串的值为 null,则它将返回 ArgumentNullException
,应使用 try-catch
模块进行处理。
using System;
public class Conversion {
public static void Main() {
string CurrentDate = "06/04/2020";
// Use of DateTime.Parse()
DateTime DateObject = DateTime.Parse(CurrentDate);
Console.WriteLine("The Date is: " + DateObject.Day + " " + DateObject.Month + " " +
DateObject.Year);
}
}
输出:
The Date is: 4 6 2020
C# 使用 DateTime.ParseExact()
将 string
转换为 DateTime
DateTime.ParseExact()
的语法是,
DateTime.ParseExact(dateTobeConverted, dateFormat, cultureInfo);
DateTime.ParseExact()
是将字符串转换为 DateTime
的最佳方法。在此方法中,我们将日期的格式作为参数传递。这使用户易于准确执行转换。
在这里,我们通过传递 null
作为参数来代替文化信息,因为它是一个全新的主题,并且需要花费一些时间来理解。
using System;
public class Conversion {
public static void Main() {
string CurrentDate = "06/04/2020";
// Use of DateTime.ParseExact()
// culture information is null here
DateTime DateObject = DateTime.ParseExact(CurrentDate, "dd/MM/yyyy", null);
Console.WriteLine("The Date is: " + DateObject.Day + " " + DateObject.Month + " " +
DateObject.Year);
}
}
输出:
The Date is: 6 4 2020
结论
有许多方法可以在 C# 中将字符串转换为 DateTime
。将字符串转换为 DateTime
的最佳方法是 DateTime.ParseExact()
。
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径