-1
當我在我的UIDatePicker(僅限時間模式)中選擇任何時間時,我得到此字符串「2015年4月27日星期一上午12時12分10秒印度標準時間」。如何從字符串中獲得長格式的時間「2015年4月27日星期一上午12點12分10秒印度標準時間」?
當我做
`NSLog(@"%@",[self.timePicker.date descriptionWithLocale:[NSLocale currentLocale]]);`
我可以看到我選擇的時間與日期,並在控制檯其他時區的詳細信息。
Monday, 27 April 2015 12:12:10 am India Standard Time
我如何只提取時間部分和將時間部分(12:12:10 am)轉換爲長格式?
代碼的NSDate轉換爲長格式
long timeInLong=[date timeIntervalSince1970]*1000;
但請告訴我怎麼去一部分時間從控制檯的字符串。
我已經試過這和Liam讓 「空」 作爲輸出
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"HH:mm aa"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:[self.timePicker.date descriptionWithLocale:[NSLocale currentLocale]]];
NSLog(@"%@",dateFromString); //null
請幫
更新:
現在我得到這樣的輸出與下面的代碼
output 2015-04-27 13:17:56 +0000
CODE:
NSDate* sourceDate = self.timePicker.date;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSString *dateOutput = [[NSString alloc] initWithFormat:@"%@", destinationDate];
NSLog(@"%@",dateOutput);
現在我仍然可以分開那個時間部分。
你想從你輸入的日期時間?或者你的意見是什麼?你需要輸出什麼? – Yuyutsu
是的,我想在NSDate 12:12上午(不含日期部分) –
'NSDate'對象表示一個時間點,它總是由日期和時間組成。 「沒有日期部分的''NSDate'」就沒有這種東西了。現在,當你只用時間表示這個'NSDate'作爲一個字符串時,你可以創建一個只輸出時間字符串的格式器。 - 也許你應該退後一步並解釋你想要解決的更廣泛的問題,我們可能會提供更好的建議。 – Rob