码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取当前时间

时间:2015-08-04 21:09:18      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:category   ios   

首先-你可以建一个类目:

//

//  NSString+DateStr.m

//  获取当前时间Demo

//

//  Created by yons on 15/7/16.

//  Copyright (c) 2015 yons. All rights reserved.

//


#import "NSString+DateStr.h"

#import <CommonCrypto/CommonDigest.h>



@implementation NSString (DateStr)


//-----周几---

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {

    

   // NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

     NSArray *weekdays = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];

    

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

    

    [calendar setTimeZone: timeZone];

    

    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;

    

    //NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];

     NSDateComponents *theComponents = [[NSCalendar autoupdatingCurrentCalendar] components:calendarUnit fromDate:inputDate];

    NSLog(@"%@",[NSString stringWithFormat:@"%i",(int)theComponents.weekday]);

    

    //return [weekdays objectAtIndex:theComponents.weekday];

    

    //return [NSString stringWithFormat:@"%i",(int)theComponents.weekday];

    

    NSString *str = [NSString stringWithFormat:@"%@",[weekdays objectAtIndex:(int)theComponents.weekday - 1]];

     

                     return str;

                     

   

    

}


//清除字符串中的空白字符

-(NSString*)trimString

{

    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

}


+ (NSString *)monthDayFromeStr:(NSInteger)inter

{



    NSString *str = [NSString stringWithFormat:@"%ld",inter];

    return str;

    

}


//计算几月几号

+ (NSString *)dateFromeString:(NSDate *)date

{

  

    NSCalendar *calendar = [NSCalendar currentCalendar];

    

    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    

    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:date];

    

    //NSInteger year = [dateComponent year];

    NSInteger month = [dateComponent month];

    NSInteger day = [dateComponent day];

    //NSInteger hour = [dateComponent hour];

    //NSInteger minute = [dateComponent minute];

    //NSInteger second = [dateComponent second];

    //NSInteger  week = [dateComponent weekday];


    NSString *str = [NSString stringWithFormat:@"%ld%ld",month,day];

    

    return str;


}


@end


建一个controller,使用的时候就简单了----

 NSString *sixStr = [NSString dateFromeString:sixDate];//几月几号

 NSString *sixWeek = [NSString weekdayStringFromDate:sixDate];//周几






版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS 获取当前时间

标签:category   ios   

原文地址:http://blog.csdn.net/zhaoguodongios/article/details/47281355

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!