Languages
[Edit]
EN

C# / .NET - convert DateTime to string fromat

2 points
Created by:
Marcino
720

In C# / .NET it is possible to convert DateTime object to string with formatting in few ways.

1. DateTime.ToString method example

Edit

Output:

Notes: 
  • this example has been run with independent culture.
  • DateTime.ToString method uses format description - see for examples bellow

2. String.Format Method examples

Edit

2.1. Usage example

Output:

Notes: 
  • this example has been run with independent culture.
  • String.Format method uses argument index and format description delimited with ":" character - see for examples bellow

2.2. Own custom formats

Edit
Notes:
  • To escape some phrases, apostrophes should be used like in this example:
    Where M by default represents number of month in DateTime.ToString format.
  • For safe, each phrases that should be not interpreted should be placed between apostrophes.

References

Edit
  1. DateTime.ToString Method - Microsoft Docs
  2. String.Format Method - Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - DateTime conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join