Koushik Saha
Koushik Saha

Reputation: 683

Format Datetime Object of type Datetime

I want to convert a DateTime instance (DataTime.Now) in YYYY-MM-DD HH:MM:SS.

The output should remain a DateTime, not a string. How to do this?

Upvotes: 1

Views: 178

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

It is not possible.

DateTime structure doesn't have any implicit format. It just have date and time values. You can only have a format with textual representation which is string representation.

The concept of a format only applies when you're converting to a string.

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 156948

That is not possible. A DateTime is a struct containing numerical values, it does not contain any formatting information. That information is stored in the CurrentCulture, specifically the DateTimeFormat.

The information in the CurrentCulture is shared across the entire thread, so if you do change that format, it will change for your entire application. If you want to format just that instance, use ToString with your appropriate format.

Upvotes: 1

Related Questions