获得系统时间的日期

[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"]

 [dateformatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"]

2014年12月30号结果显示为2015年12月30号改成yyyy就没问题了 

上面YYYY是week year

 具体区别:@"YYYY-MM-dd"是以周计算的。只是有一点要特别注意:以后转换日期格式的时候记得还是用@“yyyy-MM-dd”这种格式吧,避免出现这种情况衍生的bug。 


新建项目,- (void)viewDidLoad方法调用[self one]
- (void)viewDidLoad
{
    [superviewDidLoad];
    [self one];
}

#pragma mark -获得系统时间的日期
#pragma mark 方法1
-(void)one
{
    //获得系统时间
    NSDate * senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatteralloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
    
//NSString * morelocationString=[dateformatter stringFromDate:senddate];
    NSLog(@"%@",locationString);
    
    UILabel *sjLabel=[[UILabel alloc]initWithFrame:CGRectMake(0010090)];
    [sjLabel setText:locationString];
    [self.view addSubview:sjLabel];
    
    //获得系统日期
    NSCalendar * cal=[NSCalendarcurrentCalendar];
    NSUInteger unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year=[conponent year];
    NSInteger month=[conponent month];
    NSInteger day=[conponent day];
    NSString *nsDateString=[NSString stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
    NSLog(@"%@",nsDateString);
}

//#pragma mark 方法2
//-(void)two
//{
//    //从字符串来获得NSDate
//    string strYear="1988";
//    string strMonth="09";
//    string strDay="18";
//    string strHour="5";
//    string strMinutes="18";
//    string strSec="20";
//    morelocationString=[NSString stringWithFormat:@"%s-%s-%s-%s-%s-%s",strYear.c_str(),strMonth.c_str(),
//                        strDay.c_str(),strHour.c_str(),strMinutes.c_str(),strSec.c_str()];
//    //根据时间字符串获得NSDate
//    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
//    [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
//    NSDate * oldDate=[dateformatter dateFromString:morelocationString];
//}
//#pragma mark 方法3
//-(void)three
//{
//    //从GMT时间,得到本地时间
//    NSDate *oldDate=[dateformatter dateFromString:morelocationString];
//    NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
//    NSDate *newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];
//}
这个是从网上找的并且整理的,但是我用这个方法2和方法3始终不能实现,由于没有怎么思考,
学习的东西太多,没有太多事件思考,又快要找实习工作了。也就没弄清楚,暂且用一个方法,等学了差不多的时候再去回头思考查询。
                      2013年8月7日,12:22,东南大学无锡分校桃园3宿舍106室

原文地址:https://www.cnblogs.com/ioschen/p/3248857.html