Remove +07.00 from dotnet datetime

A colleague came to ask about generating BAHTNET (PACS.008) files where the date has +07:00 appended, for example 2026-05-22T12:06:09+07:00, and they wanted to know how to remove the +07:00. So I'm writing a quick recap of the solution here.

  1. Using DateTimeOffset - If you are working with DateTimeOffset, you can convert it to a "Unspecified" or "Local" DateTime to drop the explicit offset display. 
DateTimeOffset dto = DateTimeOffset.Now; // e.g., 2024-05-22 12:00:00 +07:00
DateTime dt = dto.DateTime; // Result: 2024-05-22 12:00:00 (Offset is gone)
  1. Create a "Clean" DateTime by Strip the Offset  - If you have a DateTime that includes UTC information and you want a version without it, you can create a new instance using only its ticks. This effectively strips the UTC/offset metadata. 
DateTime dateWithOffset = DateTime.Now; // Includes local offset info
DateTime cleanDate = new DateTime(dateWithOffset.Ticks); 

// String output will no longer include the +07:00 offset
Console.WriteLine(cleanDate.ToString("o")); 
  1. Subtract 7 Hours (Adjust the Time / Not Recommend) - use the AddHours method with a negative value. I not recommend because +07.00 is Indochina Time - ICT. if you deploy app on another timezone is not good to do this.
DateTime adjustedDate = originalDate.AddHours(-7);
  1. Remove Offset from String Output - use a Custom Date and Time Format String that excludes the z or K specifiers
// "o" or "K" includes offset; "yyyy-MM-dd HH:mm:ss" does not.
string formatted = dateAndTime.ToString("yyyy-MM-dd HH:mm:ss"); 

Happy Coding


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.