C# 将字符串转换为日期时间

您所在的位置:网站首页 文本转换时间 C# 将字符串转换为日期时间

C# 将字符串转换为日期时间

2024-07-15 00:01:43| 来源: 网络整理| 查看: 265

C# 使用 Convert.ToDateTime() 将字符串转换为 DateTime C# 使用 DateTime.Parse() 将字符串转换为 DateTime C# 使用 DateTime.ParseExact() 将 string 转换为 DateTime 结论

在大多数情况下,我们以字符串形式获取日期,并且希望分别使用日,月和年。不用担心,在 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()。



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭