
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
String format for DateTime in C#
Format DateTime using String.Format method.
Let us see an example −
Example
using System; static class Demo { static void Main() { DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123); Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d)); Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d)); Console.WriteLine(String.Format("{0:d dd ddd dddd}", d)); } }
Above we have first set the DateTime class object −
DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);
To format, we have used the String.Format() method and displayed date in different formats.
String.Format("{0:y yy yyy yyyy}", d) String.Format("{0:M MM MMM MMMM}", d) String.Format("{0:d dd ddd dddd}", d
- Related Articles
- String format for Double in C#
- How to Convert Text Datetime Format to Real Datetime Format in Excel?
- How to convert string to 24-hour datetime format in MySQL?
- How to convert C# DateTime to “YYYYMMDDHHMMSS” format?
- What is the correct DateTime format for a MySQL Database?
- Insert current date in datetime format MySQL?
- How do I re-format datetime in MySQL?
- How to Convert yyyymmddhhmmss Date Format to Normal Datetime in Excel?Datetime in Excel?
- Input type DateTime Value format with HTML
- Convert \"unknown format\" strings to datetime objects in Python
- Add10 minutes to MySQL datetime format?\n\n
- Adding a day to a DATETIME format value in MySQL?
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- How do you display JavaScript datetime in 12hour AM/PM format?
- Format String Vulnerability and Prevention with Example in C

Advertisements