shortest answer would be the code below:
DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
DateTime.Now
gets the current date and time based on your computer's clock.
.ToString(...)
converts the DateTime object into a string with optional formatting inside as parameters.
"yyyyMMddHHmmss"
is a pattern for how you want the DateTime object be mapped in a string manner where. assuming your computer's clock is currently ticked at "August 8, 2019 12:34:56 PM", then:
- yyyy = is a 4 digit year as 2019
- MM = is a 2 digit month equivalent such as 08
- dd = is a 2 digit day of the year such as 08
- HH = is a 2 digit hour equivalent in 24 Hours format such as 12
- mm = is a 2 digit minute such as 34
- ss = is a 2 digit second such as 56
and the output would be 20190808123456
. Note that the arrangement of year, month, date, hour, minute, or even second can be in no specific order.
CultureInfo.InvariantCulture
is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings (via source)
note that we removed special characters separating different parts of the DateTime object to prevent issues when filenames on Windows.