iOS:日期格式 - 20-Sep-2012
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13506102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS : Date format - 20-Sep-2012
提问by Dev
I want to display the date in the format "20-Sep-2012" from the string "2012-11-22 10:19:04". How can i do this? Is there any in-built method for iOS?
我想从字符串“2012-11-22 10:19:04”中以“20-Sep-2012”格式显示日期。我怎样才能做到这一点?iOS 有内置方法吗?
回答by Dinesh Raja
NSString *myString = @"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = @"dd-MMM-yyyy";
NSLog(@"%@",[dateFormatter stringFromDate:yourDate]);
your log will print like this. 22-Nov-2012
你的日志会像这样打印。 22-Nov-2012
回答by iDev
Try this,
尝试这个,
NSString *originalDateString = @"2012-11-22 10:19:04";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//2012-11-22 10:19:04
Then use it to parse the date string as,
然后用它来解析日期字符串,
NSDate *date = [dateFormatter dateFromString:originalDateString];
You can create a new dateformatter to print in the new format or just reuse the existing one as,
您可以创建一个新的 dateformatter 以新格式打印或仅重用现有格式,
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];//22-Nov-2012
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"Date in new format is %@", formattedDateString);
回答by Gent Berani
Accepted answer from (@Dinesh Raja) in Swift 3:
在Swift 3 中接受了 (@Dinesh Raja) 的回答:
override func viewDidLoad() {
super.viewDidLoad()
print(self.getFormattedDate(myString: "2012-11-22 10:19:04"))
}
func getFormattedDate(myString: String) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let yourDate = dateFormatter.date(from: myString)!
dateFormatter.dateFormat = "dd-MMM-yyyy"
return dateFormatter.string(from: yourDate)
}
回答by Paras Joshi
try this
尝试这个
NSString *str = @"2012-11-22 10:19:04";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);// Print Log (Output) is 22-Nov-2012
see my blog with this all information My Blog Link with this types of method
使用此所有信息查看我的博客我的博客链接与这种类型的方法
回答by Niru Mukund Shah
You can achieve this with following way
您可以通过以下方式实现此目的
NSDateFormatter *dfTime = [[NSDateFormatter alloc] init];
[dfTime setDateFormat:@"yyyy--MM-dd HH:mm:ss"];
NSDate *dtTime = [dfTime dateFromString:<Your string>];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MMM-yyyy"];
<Your result string> = [df stringFromDate:dtTime];
Where <Your string> = @"2012-11-22 10:19:04" &
<Your result string> will be your new string
Enjoy Programming
享受编程