Formatting and Parsing in C# example

Formatting and Parsing in C# example
In this article [Show more]

    Formatting and Parsing in C#

    In C#, formatting and parsing are essential skills for handling data input and output, especially when working with types like DateTime and strings. Properly managing these operations allows for more robust, error-free applications. This article will explore how to format and parse DateTime objects and strings in C# through various examples.

    DateTime.Parse Format DD/MM/YYYY

    To parse a string into a DateTime object using the format "dd/MM/yyyy", you can use the DateTime.Parse method. This method assumes the date format based on the system's culture settings, so it's essential to ensure the format matches the expected input:

     

    string date = "31/12/2020";
    DateTime dt = DateTime.Parse(date, CultureInfo.CreateSpecificCulture("en-GB"));
    Console.WriteLine(dt); // Outputs "12/31/2020 12:00:00 AM" in an en-US culture
    

    C# DateTime Parse Format YYYYMMDDHHMMSS

    To parse a datetime string formatted as "yyyyMMddHHmmss", you would typically use DateTime.ParseExact to specify the exact format:

     

    string dateTime = "20201231115959";
    DateTime parsedDateTime = DateTime.ParseExact(dateTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
    Console.WriteLine(parsedDateTime); // Outputs "12/31/2020 11:59:59 AM"
    

    DateTime Parse with Format C#

    Using DateTime.ParseExact allows you to define the exact format of the input string to ensure correct parsing:

     

    string customDate = "2020-12-31";
    DateTime dateResult = DateTime.ParseExact(customDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
    Console.WriteLine(dateResult); // Outputs "12/31/2020 12:00:00 AM"
    

    C# Parse String Format

    Parsing strings doesn't always relate directly to dates and times. You can parse other data types from strings using methods like int.Parse or double.Parse:

     

    string number = "12345";
    int result = int.Parse(number);
    Console.WriteLine(result); // Outputs 12345
    

    C# DateTime.Parse Format YYYY-MM-DD

    To parse a date string formatted as "yyyy-MM-dd", you use the DateTime.Parse or DateTime.ParseExact method:

     

    string isoDate = "2020-12-31";
    DateTime dtIso = DateTime.ParseExact(isoDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
    Console.WriteLine(dtIso); // Outputs "12/31/2020 12:00:00 AM"
    

    C# Reverse String Format

    Reversing a string format involves creating a new string with characters in reverse order:

     

    string original = "hello";
    string reversed = new string(original.Reverse().ToArray());
    Console.WriteLine(reversed); // Outputs "olleh"
    

    C# DateTime Parse Default Format

    The default format for DateTime.Parse is determined by the current culture. To parse a date in a culture-agnostic way, use CultureInfo.InvariantCulture:

     

    string defaultDate = "01/31/2020"; // Assumes the current culture is en-US
    DateTime defaultDateTime = DateTime.Parse(defaultDate, CultureInfo.InvariantCulture);
    Console.WriteLine(defaultDateTime); // Outputs "1/31/2020 12:00:00 AM"
    

    DateTime ParseExact C#

    DateTime.ParseExact allows you to specify the exact format of the date string you expect, which helps avoid parsing errors due to unexpected formats:

     

    string exactDate = "Friday, 31 December 2020";
    DateTime exactDateTime = DateTime.ParseExact(exactDate, "dddd, dd MMMM yyyy", CultureInfo.InvariantCulture);
    Console.WriteLine(exactDateTime); // Outputs "12/31/2020 12:00:00 AM"
    

    Formatting and parsing in C# are powerful tools that, when used correctly, can significantly simplify data handling, ensuring that data is displayed in a user-friendly manner and that input data conforms to the expected format.


     

    Author Information
    • Author: Ehsan Babaei

    Send Comment



    Comments