字符串转换为格式yyyy-MM-dd HH:mm:ss Iphone [英] String to a Format yyyy-MM-dd HH:mm:ss Iphone

查看:109
本文介绍了字符串转换为格式yyyy-MM-dd HH:mm:ss Iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个nsstring,见下文

I have an nsstring ,see below

NSString *Mydate=@"9/8/2011";以月/日/年的格式.

NSString *Mydate=@"9/8/2011"; in month/day/year format.

我希望此字符串的格式为yyyy-MM-dd HH:mm:ss.

I want this string to be in the format yyyy-MM-dd HH:mm:ss.

例如:2011-09-08 15:51:57

for eg: 2011-09-08 15:51:57

所以我需要以更高的格式在标签中显示字符串.

so that i need to show the string in a label in the later format.

谢谢.

推荐答案

尝试使用有效的输入字符串尝试以下代码,这将对您有所帮助.

Try below code with valid input string that will help you.

    NSString *dateStr = @"9/8/2011 11:10:9";
    NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
    [dtF setDateFormat:@"d/M/yyyy hh:mm:s"];
    NSDate *d = [dtF dateFromString:dateStr];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:s"];
    NSString *st = [dateFormat stringFromDate:d];   
    NSLog(@"%@",st);
    [dtF release];
    [dateFormat release];

这篇关于字符串转换为格式yyyy-MM-dd HH:mm:ss Iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆