-1

when i run the below code, string dt = "2017-07-09T17:50:21.000-0500"; DateTime date = Convert.ToDateTime(dt);

it gives me output as

7/10/2017 4:20:21 AM

where as i want my output to be

2017-07-09 17:50


update

the code @alexander-petrov gave worked string dt = "2017-07-09T17:50:21.000-0500"; string date = DateTimeOffset.Parse(dt).DateTime.ToString("yyyy-MM-dd HH:mm");

gives output

2017-07-09 17:50

but on inserting the same to database it is adding +5 hrs to the time and inserting as

2017-07-09 22:50

stacker
  • 19
  • 6
  • 5
    "It is showing wrong time" - what time do you _expect_ it to be? It's a [standard format](https://en.wikipedia.org/wiki/ISO_8601) - you need to show what you're doing to parse it, and why you think it's different to what it should be. – James Thorpe Jun 09 '17 at 12:48
  • 2017-07-09T17:50:21.000-0500 is in string? How are you converting it into DateTime? Can you share that code? – Yogesh Patel Jun 09 '17 at 12:49
  • 1
    Probably a problem with timezones. Please post your conversion code. – Myrtle Jun 09 '17 at 12:49

4 Answers4

1

This is a Round-Trip format of a DateTime specified with a DateTimeKind.Local kind.
You need to decide if your program needs to be aware of time zones or not.

You could try parsing it while supplying the System.Globalization.DateTimeStyles.RoundtripKind or System.Globalization.DateTimeStyles.AdjustToUniversal parameter to the Parse method.

Jony Adamit
  • 3,178
  • 35
  • 44
0

I couldn't get your date to work, as I think there is a colon missing in the last part. Adding that colon back allows me to convert the XSD date time into a SQL DATETIME using this script:

DECLARE @stringDate VARCHAR(30);
SELECT @stringDate = '2017-07-09T17:50:21.000-05:00';
DECLARE @xmlDate XML;
SELECT @xmlDate = CAST('' AS XML);
SELECT @xmlDate.value('xs:dateTime(sql:variable("@stringDate"))', 'datetime');

Results:

2017-07-09 22:50:21.000
Richard Hansell
  • 5,315
  • 1
  • 16
  • 35
0

Try:

string date = "2017-07-09T17:50:21.000-0500";
DateTime d = DateTime.ParseExact(date, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzzz", null);
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
0

If you want take offset into account then use DateTimeOffset type.

string dt = "2017-07-09T17:50:21.000-0500";

DateTimeOffset date = DateTimeOffset.Parse(dt);

// format on my machine
// 09.07.2017 17:50:21 - 05:00
Console.WriteLine(date);

// without offset
// 09.07.2017 17:50:21
Console.WriteLine(date.DateTime);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • on converting the same to DateTime result is changing, i need to save // // 09.07.2017 17:50 in database – stacker Jun 09 '17 at 18:10
  • @stacker - Don't convert to `DateTime`. Always use `DateTimeOffset`. [Must read](https://stackoverflow.com/q/4331189/5045688). – Alexander Petrov Jun 09 '17 at 18:14
  • the code you gave worked `string dt = "2017-07-09T17:50:21.000-0500"; string date = DateTimeOffset.Parse(dt).DateTime.ToString("yyyy-MM-dd HH:mm");` gives output >> 2017-07-09 17:50 but on inserting the same to database it is adding 5hrs to the time and inserting as >> 2017-07-09 22:50 – stacker Jun 10 '17 at 03:20
  • @stacker - ask another question about inserting into database. – Alexander Petrov Jun 10 '17 at 10:14
  • ok..added [here](https://stackoverflow.com/questions/44486866/inserting-iso-date-in-sql-server) – stacker Jun 11 '17 at 17:49