2011-06-15 79 views
3

我正在從帶有dateTime數據類型參數的iPhone應用程序調用.net Web服務。我想通過當前日期。我想知道我需要使用哪種日期格式。 我會感激你的幫助, 感謝將NSDate轉換爲Sql Server日期時間格式

NSDate *now=[NSDate date]; 
+0

你解決了嗎?我也面臨同樣的問題。我想從webservice發送當前日期作爲參數。我怎樣才能做到這一點? – 2013-07-16 10:04:16

回答

3

更獨立的方式將排在第二,而不是任何格式,以您的Web服務器發送時間,

NSDate *now = [NSDate date]; 
NSTimeInterval inSecond = [now timeIntervalSince1970]; 

inSecond是double類型,並讓您的Web服務器計算您發送的秒數。

0
NSDATE *date; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"ddMMyyyy"]; 
NSString *textDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]]; 
[dateFormatter release]; 
3
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; 
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithname:@"UTC"]]; 
NSString *sqlDate = [dateFormatter stringFromDate: date]; // date is your NSDate object 

編輯:更新答案把時區考慮在內。顯然你應該使用原始日期使用的相同時區。

相關問題