Member-only story

DateTime in C#

Shahzad Aslam
4 min readMay 30, 2024

--

In C#, the DateTime struct is a fundamental data type used for working with dates and times. It is part of the System namespace and provides a wide range of methods and properties for handling date and time information

DateTime in C#

Features with datetime in C#:

Creating a DateTime Object:

You can create a DateTime object to represent a specific date and time. There are several constructors available, but the most common one takes year, month, day, hour, minute, and second as parameters:

Example

using System;
DateTime dateTime = new DateTime(2023, 10, 31, 14, 30, 0);

Current Date and Time:

To get the current date and time, you can use the DateTime.Now property:

Example

DateTime currentDateTime = DateTime.Now;

Date and Time Formatting:

You can format a DateTime object into a string using the ToString method or specifying a format string:

Example

string formattedDateTime = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");

Date and Time Properties:

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

Responses (1)