-1

I have date field in database with 'datetimeoffset(7)'. It looks like

2021-09-30 02:00:31.3000000 +00:00.

As I understand it uses this structure:

YYYY-MM-DD hh:mm:ss[.nnnnnnn] [{+|-}hh:mm]

I've tried this but "Now" is underlined :

var currentDate = DateTimeOffset.Now("YYYY-MM-DD hh:mm:ss[.nnnnnnn] [{+|-}hh:mm]");

How can I get current date with this date structure?

Natali
  • 45
  • 5
  • 1
    Check example on [MSDN page](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-5.0) . `DateTimeOffset.Now` is a property that will return a value of current date-time value in UTC. If you want to format to a string, you should try to use `DateTimeOffset.Now.ToString()` – Tatranskymedved Sep 30 '21 at 12:01
  • DateTimeOffset.Now or DateTimeOffset.Now.ToString() gives me this date 9/30/2021 3:13:35 PM +03:00 but it's not correct structure I need. – Natali Sep 30 '21 at 12:15
  • Does this answer your question? [C# DateTime to "YYYYMMDDHHMMSS" format](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – Tatranskymedved Sep 30 '21 at 12:17
  • Look at that "duplicate" to 2nd answer ( https://stackoverflow.com/a/3025389/7167572 ), it shows exactly how to format datetime to the format you want. – Tatranskymedved Sep 30 '21 at 12:17

1 Answers1

1

I belive this is format what you need:

var currentDate = DateTimeOffset.Now.ToString("yyyy-MM-dd hh:mm:ss.fffffff zzz");

https://stackoverflow.com/a/63242998/8825020

Dilshod K
  • 2,924
  • 1
  • 13
  • 46