首先-你可以建一个类目:
//
// 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
NSString *sixStr = [NSString dateFromeString:sixDate];//几月几号
NSString *sixWeek = [NSString weekdayStringFromDate:sixDate];//周几
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhaoguodongios/article/details/47281355