How to get current time in T07:07:45 0500 format

I want to get the current time in T07:07:45-0500 format.

I have try like this:

DateTime currentDate = DateTime.Now;
String currentTime = currentDate.ToString("'T'HH':'mm':'ssK");
Console.WriteLine("DOB ==> " + dateOfBirth.ToString("yyyy-MM-dd") + currentTime);

But the result is 1986-07-11T07:07:45-05:00

I want result like 1986-07-11T07:07:45-0500

Is there any way to get the current time in T07:07:45-0500 format.

Please help me to resolve my problem.

Jon Skeet
people
quotationmark

I don't think the .NET custom date and time format strings allow that format. You could use my Noda Time project easily enough, using DateTimeOffset.Now if you want the system default time zone to be applied. (It can be done explicitly

For example:

var odt = OffsetDateTime.FromDateTimeOffset(DateTimeOffset.Now);
var pattern = OffsetDateTimePattern.CreateWithInvariantCulture(
    "yyyy-MM-dd'T'HH:mm:sso<+HH:mm>");
Console.WriteLine(pattern.Format(odt));

As noted in comments, you're currently doing something very odd in terms of combining the values. If you really want to do that, I would personally create an appropriate OffsetDateTime like this:

LocalDate dateOfBirth = ...; // Wherever that comes from
OffsetDateTime now = OffsetDateTime.FromDateTimeOffset(DateTimeOffset.Now);
OffsetDateTime mixture = dateOfBirth.At(now.LocalTime).WithOffset(now.Offset);
var pattern = ...; // as before
Console.WriteLine(pattern.Format(mixture));

That makes it clearer how you're combining the values.

people

See more on this question at Stackoverflow