C# Program to Formats Data and Time Value.

Date and time values are crucial in applications for various purposes, such as displaying timestamps, scheduling events, or formatting data for reports. In C#, the DateTime structure is used to work with date and time values. You can format these values into human-readable strings using the ToString method.


The format string used with the DateTime.ToString method allows you to customize how the date and time are displayed. Here we are going to look at some examples of format strings and the resulting formatted date and time:


1. "yyyy-MM-dd HH:mm:ss": This format string displays the date and time in the format "Year-Month-Day Hour:Minute:Second."

C# Code:

// C-sharp program to print current date and time
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        DateTime dateTime = DateTime.Now;
        string formattedDateTime = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine(formattedDateTime);
    }
}
Output:
2023-09-02 14:46:04

2. "dd/MM/yyyy": This format string displays the date in the format "Day/Month/Year."

C# Code:

// C-sharp program to print current date and time
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        DateTime dateTime = DateTime.Now;
        string formattedDateTime = dateTime.ToString("dd/MM/yyyy");
        Console.WriteLine(formattedDateTime);
    }
}
Output:
25/09/2023

3. "MMMM dd, yyyy": This format string displays the date as "Month Day, Year."

C# Code:

// C-sharp program to print current date and time
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        DateTime dateTime = DateTime.Now;
        string formattedDateTime = dateTime.ToString("MMM dd, yyyy");
        Console.WriteLine(formattedDateTime);
    }
}
Output:
July 24, 2023

4. "hh:mm tt": This format string displays the time in a 12-hour clock format with "AM" or "PM."

C# Code:

// C-sharp program to print current date and time
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        DateTime dateTime = DateTime.Now;
        string formattedDateTime = dateTime.ToString("hh:mm tt");
        Console.WriteLine(formattedDateTime);
    }
}
Output:
04:40 PM

I hope you get some idea about how to get date and time values in the C# programming language. There are many different ways to customize and format the Date and Time value.

Basic Format Specifiers.

Below is the list of some commonly used Format Specifiers:
  • "d": Short date pattern (e.g., "7/25/2023").
  • "D": Long date pattern (e.g., "Monday, July 25, 2023").
  • "t": Short time pattern (e.g., "3:30 PM").
  • "T": Long time pattern (e.g., "3:30:45 PM").
  • "f": Full date and short time pattern (e.g., "Monday, July 25, 2023 3:30 PM").
  • "F": Full date and long time pattern (e.g., "Monday, July 25, 2023 3:30:45 PM").
  • "g": General date and short time pattern (e.g., "7/25/2023 3:30 PM").
  • "G": General date and long time pattern (e.g., "7/25/2023 3:30:45 PM").
  • "yyyy-MM-dd": Custom date format (e.g., "2023-07-25").
  • "HH:mm:ss": Custom time format (e.g., "15:30:45").

Custom Format Specifiers.

In addition to the predefined format specifiers, you can create custom format strings. Here's how to use some common custom specifiers:
  • "yyyy": Four-digit year (e.g., "2023").
  • "MM": Two-digit month (e.g., "07" for July).
  • "dd": Two-digit day (e.g., "25").
  • "HH": Two-digit hour (24-hour clock, e.g., "15").
  • "hh": Two-digit hour (12-hour clock, e.g., "03").
  • "mm": Two-digit minute (e.g., "30").
  • "ss": Two-digit second (e.g., "45").
  • "fff": Milliseconds (e.g., "123").

C# Code to Customize Data and Time Format.

// C-sharp program to customize current date and time
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        DateTime now = DateTime.Now;
        
        // "7/25/2023"
        string shortDate = now.ToString("d"); 
        // "Monday, July 25, 2023"
        string longDate = now.ToString("D"); 
        // "3:30 PM"
        string shortTime = now.ToString("t");
        // "3:30:45 PM"
        string longTime = now.ToString("T");
        // "Monday, July 25, 2023 3:30"
        string fullDateTime = now.ToString("F");
        // "2023-07-25 15:30:45"
        string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss"); 
        
        Console.WriteLine(shortDate);
        Console.WriteLine(longDate);
        Console.WriteLine(shortTime);
        Console.WriteLine(longTime);
        Console.WriteLine(fullDateTime);
        Console.WriteLine(customFormat);
    }
}
Output:
09/02/2023
Saturday, 02 September 2023
15:22
15:22:07
Saturday, 02 September 2023 15:22:07
2023-09-02 15:22:07

Formatting date and time values in C# is essential for displaying information to users or storing timestamps in a specific format. By using format specifiers, you can control how date and time values are presented. Understanding the available format options and creating custom formats when necessary will help you work effectively with date and time values in your C# applications.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS