Convert datetime without timezone

29,987

Solution 1

You can use the DateTime property of DateTimeOffset.

Example:

string s = "2013-07-22T08:51:38.000-07:00";
var dateTimeOffset =DateTimeOffset.Parse(s, null);
Console.WriteLine(dateTimeOffset.DateTime); 

Outputs:

22/07/2013 08:51:38

Solution 2

you can try this.

DateTimeOffset.Parse("2013-07-22T08:51:38.000-07:00").DateTime.ToString("dd-MM-yyyy hh:mm:ss tt");

Solution 3

You can try the below one

        string s = "2013-07-22T08:51:38.000-07:00";

        DateTime d = Convert.ToDateTime(s);

        Console.WriteLine(d.Date.ToShortDateString());
Share:
29,987
P_rnd
Author by

P_rnd

Updated on December 11, 2020

Comments

  • P_rnd
    P_rnd over 3 years

    I have date in string: "2013-07-22T08:51:38.000-07:00"

    When I try parse this string, I receive date with offset of timezone.

    How can I make it without timezone offset?

    ---UPDATE---

    it is that I receive: DateTime.Parse("2013-07-22T08:51:38.000-07:00") = 7/22/2013 7:51:38 PM but I need to receive 7/22/2013 8:51:38 AM - DateTime without offset.

  • P_rnd
    P_rnd over 10 years
    if it been 2013-07-22T08:51:38.000 parse works fine.
  • P_rnd
    P_rnd over 10 years
    but '2013-07-22T08:51:38.000-07:00' does offset time.
  • Gun
    Gun over 10 years
    You can use DateTime object directly Console.WriteLine(d);
  • Paul Raff
    Paul Raff over 8 years
    This is not a good answer since the date itself may be modified by the timezone.
  • Mike Christensen
    Mike Christensen about 7 years
    Plus one for this! Helped me fix a big bug with our code today!
  • ryanwebjackson
    ryanwebjackson over 6 years
    Is there a way to do this before an HTTP Handler is called?