使用NSDateFormatter发布日期格式 [英] issue with date formatting using NSDateFormatter

查看:151
本文介绍了使用NSDateFormatter发布日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串,我想转换为另一种格式。

I have a date string which I want to convert to another format.

原始日期字符串示例是:
2013-06 -04 02:19:21 +0000

The original date string example is : "2013-06-04 02:19:21 +0000"

我想将其转换为
Wed,Jun 4

NSString * date_string = @"2013-06-04 02:19:21 +0000";

NSDateFormatter *complexdateFormater = [[NSDateFormatter alloc] init];
[complexdateFormater setDateFormat:@"yyyy-M-DD HH:mm:ss Z"];
 NSDate* complexdate = [complexdateFormater dateFromString:date_string];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"EE, MMM d" options:0
                                                          locale:[NSLocale    currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:complexdate];

但上述情况的今天字符串打印出来:
当月为Jan,不管原始字符串中的月份值。

but the today string for the above case prints out : the month as Jan irrespective of the month value in the original string.

我在哪里出错?

推荐答案

这里你去我的朋友

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEE, MMM dd"];
    NSString *theDate = [dateFormat stringFromDate:date1];

可能访问此帖子的所有其他人的日期格式化程序引用。如果有帮助,请对其进行评估。

Date formatter reference for everyone else that might visit this post. Do rate it up if it helped!

/*
     x           number
     xx          two digit number
     xxx         abbreviated name
     xxxx        full name

     a           AM/PM
     A           millisecond of day
     c           day of week (c,cc,ccc,cccc)
     d           day of month
     e           day of week (e,EEE,EEEE)
     F           week of month
     g           julian day (since 1/1/4713 BC)
     G           era designator (G=GGG,GGGG)
     h           hour (1-12, zero padded)
     H           hour (0-23, zero padded)
     L           month of year (L,LL,LLL,LLLL)
     m           minute of hour (0-59, zero padded)
     M           month of year (M,MM,MMM,MMMM)
     Q           quarter of year (Q,QQ,QQQ,QQQQ)
     s           seconds of minute (0-59, zero padded)
     S           fraction of second
     u           zero padded year
     v           general timezone (v=vvv,vvvv)
     w           week of year (0-53, zero padded)
     y           year (y,yy,yyyy)
     z           specific timezone (z=zzz,zzzz)
     Z           timezone offset +0000

     sql         y-M-d H:m:s
     rss         [E, ]d MMM y[y] H:m:s Z|z[zzz]
     */

这篇关于使用NSDateFormatter发布日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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