@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel* label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 320, 30)];
label1.text = [self getNowRequestTime];
label1.textColor = [UIColor blackColor];
label1.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label1];
UILabel* label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 320, 30)];
label2.text = [self getNowPayRequestTime];
label2.textColor = [UIColor blackColor];
label2.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label2];
UILabel* label3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, 320, 30)];
label3.text = [self getNowTime];
label3.textColor = [UIColor blackColor];
label3.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label3];
// UILabel* label4 = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 320, 30)];
// label4.text = [self getPriousorLaterDateFromDate];
// label4.textColor = [UIColor blackColor];
// label4.font = [UIFont systemFontOfSize:20];
// [self.view addSubview:label4];
UILabel* label5 = [[UILabel alloc] initWithFrame:CGRectMake(30, 180, 320, 30)];
label5.text = [self getDate];
label5.textColor = [UIColor blackColor];
label5.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label5];
}
-(NSString *)getNowRequestTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* localzone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localzone];
NSDate *date = [NSDate date];
NSString *text = [dateFormatter stringFromDate:date];
return text;
}
-(NSString *)getNowPayRequestTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSTimeZone* localzone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localzone];
NSDate *date = [NSDate date];
NSString *text = [dateFormatter stringFromDate:date];
return text;
}
-(NSString *)getNowTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSTimeZone* localzone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localzone];
NSDate *date = [NSDate date];
NSString *text = [dateFormatter stringFromDate:date];
return text;
}
-(NSString *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:month];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *PriousorLaterDateString=[formatter stringFromDate: mDate];
return PriousorLaterDateString;
}
-(NSString *)getDate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
return locationString;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS之NSDate的使用
最新推荐文章于 2024-03-25 09:00:00 发布