【问题标题】:Set Formatted NSDate in UILabel text [duplicate]在 UILabel 文本中设置格式化的 NSDate [重复]
【发布时间】:2013-05-03 12:47:19
【问题描述】:

我有来自 api 并存储为 string 的日期

2013-05-09 09:30:00

我想显示为

09-05-2013 09:30

在UILabel。我想知道如何使用 NSDateFormatter。 提前致谢!

【问题讨论】:

    标签: objective-c ios6 uilabel nsdate nsdateformatter


    【解决方案1】:

    查看我对这个问题的回答,您将在此处获得所需的所有信息:

    Date formatter for the google calender API

    【讨论】:

      【解决方案2】:

      试试

              NSString *str = @"2013-05-09 09:30:00";
              NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
              [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
              NSDate *dt = [dateFormatter dateFromString:str];
      
              NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
              [dateFormatter1 setDateFormat:@"dd-MM-yyyy HH:mm"];
              NSString *str1 = [dateFormatter1 stringFromDate:dt];
              NSLog(@"Formated date - %@", str1);//output -> 09-05-2013 09:30
      
              lbl.text = str1;
      

      【讨论】:

        【解决方案3】:

        一旦经历过这个,

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy''HH:mm"];
        NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
        NSString *formattedDateString = [dateFormatter stringFromDate:date];
        NSLog(@"%@",[formattedDateString stringByReplacingOccurrencesOfString:@"'" withString:@" "]);
        

        这里 NSLog 打印像 03-01-2001 02:30 ,更多 here 是苹果文档。

        希望对你有帮助...

        【讨论】:

        • OP 希望将日期显示为标签文本。但你没有回答。
        猜你喜欢
        • 1970-01-01
        • 2022-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-24
        • 2014-09-21
        • 1970-01-01
        • 2014-04-24
        相关资源
        最近更新 更多