首页  >  文章  >  后端开发  >  C# 中的日期时间

C# 中的日期时间

WBOY
WBOY原创
2024-09-03 15:16:12284浏览

在 C# 中,DateTime 是一个结构体。因此它是值类型并用于表示瞬时时间。它用于表示一天中的日期和时间。 DateTime 类型的取值范围为 0001 年 1 月 1 日午夜 12:00:00 到 9999 年 12 月 31 日晚上 11:59:59。DateTime 的值不能为 null,因为它是值类型。要初始化 DateTime 值,我们可以调用 DateTime 构造函数的任何重载。我们还可以将从属性或方法返回的值分配给 DateTime 对象。

语法:

以下是初始化 DateTime 结构的新实例的语法:

DateTime date_time = new DateTime();

这里,date_time 是为 DateTime 类型的实例指定的用户定义名称。我们使用“new”运算符初始化了该实例。在上面的语法中,我们使用隐式无参数构造函数将 DateTime 初始化为其默认值。我们还可以使用 DateTime 构造函数的任何重载来初始化 DateTime 实例。

DateTime 在 C# 中如何工作?

在 C# 中,我们可以使用 DateTime 并通过多种方式为 DateTime 变量赋值。

  • 我们可以通过调用 DateTime 构造函数的任何重载来为 DateTime 变量赋值,无论是无参数构造函数还是带参数的构造函数,如下所示:
DateTime dateTime = new DateTime(2020, 2, 8, 7, 32, 56);

上述语句初始化特定年、月、日、小时、分钟和秒的 DateTime 结构的新实例。

  • 在这里,我们使用了以下 DateTime 结构的构造函数:
public DateTime(int year, int month, int day, int hour, int minute, int second);
  • 除了上述构造函数之外,还有其他 10 个可用于 DateTime 的构造函数,如下所示:
public DateTime(long ticks);
  • 这里,“ticks”表示日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 起经过的百纳秒间隔数表示。
public DateTime(long ticks, DateTimeKind kind);
  • 这里,‘kind’表示枚举值中的一个值,它表示刻度是否指定本地时间、协调世界时间或以上均不指定。
public DateTime(int year, int month, int day);
  • 在此上下文中,“年”代表 1 到 9999 之间的值,“月”代表 1 到 12 之间的值,“日”代表特定月份中的天数范围内的值。
public DateTime(int year, int month, int day, Calendar calendar);
  • 这里的“calendar”代表用于解释年、月、日的日历。
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
  • 这里,‘毫秒’代表0到999之间的毫秒。
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);
  • 我们可以将属性或方法返回的 DateTime 值分配给 DateTime 变量,如下所示:
DateTime dateTime = DateTime.Now;

这会将当前日期和时间分配给 DateTime 变量。

  • 我们可以从字符串表示中解析 DateTime 值,并将其分配给 DateTime 变量,如下所示:
string str = "6/2/2020 9:20:40 AM";
DateTime dateTime = DateTime.Parse(str, System.Globalization.CultureInfo.InvariantCulture);

我们可以使用 Parse()、ParseExact()、TryParse() 和 TryParseExact() 方法执行上述转换。

C# 中的日期时间示例

以下是如何将字符串解析为 DateTime 对象的几个示例:

示例#1

使用 DateTime 提供的属性和方法显示当前日期和时间以及明天的日期和时间的示例:

代码:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static DateTime GetNextDay()
{
//getting next day using AddDays() method
return DateTime.Now.AddDays(1);
}
public static void Main()
{
//displaying current date and time using 'Now' property of DateTime
Console.WriteLine("Current date and time: {0}", DateTime.Now);
DateTime dateTime = GetNextDay();
Console.WriteLine("Tomorrow date and time: {0}", dateTime);
Console.ReadLine();
}
}
}

输出:

C# 中的日期时间

示例#2

例如,将用户输入的年份作为年份,然后使用 DateTime.IsLeapYear() 方法检查是否是闰年。

代码:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static void Main()
{
try
{
//taking year as input from user
Console.WriteLine("Please enter a year");
int year = Convert.ToInt32(Console.ReadLine());
//checking if entered year is a leap year or not
//using DateTime.IsLeapYear() method
Console.WriteLine("\n Using IsLeapYear() method:");
if (DateTime.IsLeapYear(year))
{
Console.WriteLine(year + " is a leap year");
}
else
{
Console.WriteLine(year + " is not a leap year");
}
//checking if entered year is a leap year or not
//using DateTime.DaysInMonth() method
Console.WriteLine("\n Using DaysInMonth() method:");
if (DateTime.DaysInMonth(year, 2) == 29)
{
Console.WriteLine(year + " is a leap year");
}
else
{
Console.WriteLine(year + " is not a leap year");
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}

输出:

C# 中的日期时间

示例#3

例如,我们获取一年中的第一天和最后一天。

代码:

using System;
using System.IO;
namespace ConsoleApp4
{
class Program
{
public static void Main()
{
DateTime dateTime = DateTime.Now;
//displaying first day of current year
DateTime firstDay = new DateTime(dateTime.Year, 1, 1);
Console.WriteLine("First day of {0} is {1}", dateTime.Year, firstDay);
//getting first day of next year
DateTime dateTimeNext = new DateTime(dateTime.Year + 1, 1, 1);
//subtracting one day from the first day of next year
//to get the last day of current year
DateTime lastday = dateTimeNext.AddDays(-1);
Console.WriteLine("Last day of {0} is {1}", dateTime.Year, lastday);
Console.ReadLine();
}
}
}

输出:

C# 中的日期时间

结论

dateTime 结构用于处理日期和时间。它用作存储日期和时间的数据类型。 DateTime 提供了处理日期和时间的属性和方法。 DateTime 是一个结构体,并且是一个值类型;它不能为空。

以上是C# 中的日期时间的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn