我需要确定一个特定的日期时间格式

时间:2016-02-09 09:37:04

标签: c# datetime-format

以下日期时间字符串的格式是什么?我需要在C#中阅读它。

2010-09-29T02:40:00.2291503+05:30

2 个答案:

答案 0 :(得分:1)

您可以使用DateTime.Parse("2010-09-29T02:40:00.2291503+05:30");

解析字符串
//Method to return a status code
        func responseCode() -> Int{
        var responseCode : Int=0
        if Reachability.isConnectedToNetwork() == true {
        // get news feed url
        let url = NSURL(string: baseUrl)
        let session = NSURLSession.sharedSession()
//create request
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = httpMethod
//check null value
        if (bodyData != "") {
            request.HTTPBody =  bodyData.dataUsingEncoding(NSUTF8StringEncoding);
        }
        if (auth != "") {
            print("Token Auth : \(auth)")
            request.addValue("bearer \(self.auth)", forHTTPHeaderField:      "Authorization")
        }
        print("request : \(request)")

//How to return a Integer from this below function
        let task = session.dataTaskWithRequest(request,     completionHandler: {(data, response, error) -> Void in
            print("Error\(error)")
            if let httpResponse = response as? NSHTTPURLResponse {
                print("HttpReponse\(httpResponse.statusCode)")
                // Get a status code and assign it in a variable
                responseCode = httpResponse.statusCode
            }
        })
        task.resume()
//Return a response code in ViewController but it just return a initial set value that is 0
        return responseCode
        }
//Return a responseCode in ViewController but it just return a    initial set value that is 0
    return responseCode
    }

答案 1 :(得分:1)

这是带有RoundTrip

的日期时间
  

“O”或“o”标准格式说明符(和   “yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'。'fffffffK”自定义格式字符串)   ISO 8601代表时区的三种方式的优势   保留DateTime值的Kind属性的信息:

     
      
  • DateTimeKind.Local日期和时间值的时区组件是与UTC的偏移量(例如,+ 01:00,-07:00)。所有   DateTimeOffset值也以此格式表示。

  •   
  • DateTimeKind.Utc日期和时间值的时区组件使用“Z”(代表零偏移)来表示UTC。

  •   
  • DateTimeKind.Unspecified日期和时间值没有时区信息。

  •   

以下是使用DateTime.Kind的示例:

using System;

public class Example
{
   public static void Main()
   {
       DateTime dat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                   DateTimeKind.Unspecified);
       Console.WriteLine("{0} ({1}) --> {0:O}", dat, dat.Kind); 

       DateTime uDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Utc);
       Console.WriteLine("{0} ({1}) --> {0:O}", uDat, uDat.Kind);

       DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Local);
       Console.WriteLine("{0} ({1}) --> {0:O}\n", lDat, lDat.Kind);

       DateTimeOffset dto = new DateTimeOffset(lDat);
       Console.WriteLine("{0} --> {0:O}", dto);
   }
}
// The example displays the following output:
//    6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000
//    6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z
//    6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00
//    
//    6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00