转换为UTC时间

时间:2017-01-17 04:33:59

标签: c# datetime fedex

我正在尝试将我的数据转换为DateTimeAssumeUniversal,但我收到错误

  

字符串未被识别为有效的DateTime

这是

中传递的格式
1/10/2017 1:13:00 PM

这是我的跟踪事件

public partial class TrackEvent {
    private System.DateTime timestampField;
    private bool timestampFieldSpecified;
    private string eventTypeField;
    private string eventDescriptionField;
    private string statusExceptionCodeField;
    private string statusExceptionDescriptionField;
    private Address addressField;
    private string stationIdField;
    private ArrivalLocationType arrivalLocationField;
    private bool arrivalLocationFieldSpecified;

    /// <remarks/>
    public System.DateTime Timestamp {
        get {
            return this.timestampField;
        }
        set {
            this.timestampField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TimestampSpecified {
        get {
            return this.timestampFieldSpecified;
        }
        set {
            this.timestampFieldSpecified = value;
        }
    }

    /// <remarks/>
    public string EventType {
        get {
            return this.eventTypeField;
        }
        set {
            this.eventTypeField = value;
        }
    }

    /// <remarks/>
    public string EventDescription {
        get {
            return this.eventDescriptionField;
        }
        set {
            this.eventDescriptionField = value;
        }
    }

    /// <remarks/>
    public string StatusExceptionCode {
        get {
            return this.statusExceptionCodeField;
        }
        set {
            this.statusExceptionCodeField = value;
        }
    }

    /// <remarks/>
    public string StatusExceptionDescription {
        get {
            return this.statusExceptionDescriptionField;
        }
        set {
            this.statusExceptionDescriptionField = value;
        }
    }

    /// <remarks/>
    public Address Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
        }
    }

    /// <remarks/>
    public string StationId {
        get {
            return this.stationIdField;
        }
        set {
            this.stationIdField = value;
        }
    }

    /// <remarks/>
    public ArrivalLocationType ArrivalLocation {
        get {
            return this.arrivalLocationField;
        }
        set {
            this.arrivalLocationField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ArrivalLocationSpecified {
        get {
            return this.arrivalLocationFieldSpecified;
        }
        set {
            this.arrivalLocationFieldSpecified = value;
        }
    }
}

这就是我试图调用转换的方式:

TrackEvent currMax = new TrackEvent();
currMax.Timestamp = DateTime.MinValue;
foreach (TrackEvent trackevent in trackDetail.Events)
{  
 if (trackevent.TimestampSpecified && trackevent.Timestamp > currMax.Timestamp)
{
  currMax = trackevent;                 
  var formats = new[]
  {
    "MM/dd/yyyy hh:mm tt",
    "MM/dd/yyyy hh:mmtt",
    "MM/dd/yyyy h:mm tt",
    "MM/dd/yyyy h:mmtt",
    "MM/dd/yyyy hhtt",
    "MM/dd/yyyy htt",
    "MM/dd/yyyy h tt",
    "MM/dd/yyyy hh tt",
    "MM/dd/yyyy HH:mm:ss.fff",
    "MM/dd/yyyy h:mm:ss tt"
  };
    DateTime time = DateTime.ParseExact(currMax.Timestamp.ToString(), formats, System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.AssumeUniversal |      System.Globalization.DateTimeStyles.AdjustToUniversal);
    Console.WriteLine(time);
  }
}

1 个答案:

答案 0 :(得分:2)

您使用DateTime.ParseExact查看所有给定格式,将string解析为DateTime

但输入datetime格式

1/10/2017 1:13:00 PM

本月只有一个数字(即M/dd ...),而formats所有datetime用{{解析输入DateTime.ParseExact 1}}仅支持日/月的两个数字(即:MM/dd ...):

  var formats = new[]
  {
    "MM/dd/yyyy hh:mm tt",
    "MM/dd/yyyy hh:mmtt",
    "MM/dd/yyyy h:mm tt",
    "MM/dd/yyyy h:mmtt",
    "MM/dd/yyyy hhtt",
    "MM/dd/yyyy htt",
    "MM/dd/yyyy h tt",
    "MM/dd/yyyy hh tt",
    "MM/dd/yyyy HH:mm:ss.fff",
    "MM/dd/yyyy h:mm:ss tt"
  };

请注意,您在所有格式中使用MM/dd ...

要更正它,您还应该在formats中包含允许单个数字作为日/月表示的格式:

   var formats = new[]
  {
    "M/d/yyyy hh:mm tt",
    "M/d/yyyy hh:mmtt",
    "M/d/yyyy h:mm tt",
    "M/d/yyyy h:mmtt",
    "M/d/yyyy hhtt",
    "M/d/yyyy htt",
    "M/d/yyyy h tt",
    "M/d/yyyy hh tt",
    "M/d/yyyy HH:mm:ss.fff",
    "M/d/yyyy h:mm:ss tt"
  };

上述格式将支持单个数字两位数的日期和月份