日期显示不正确
date not showing in correct manner
我有一些 7 UILabels
会显示如下日期:
Jan,2016 (label)
(button) < **29Dec 30Dec 31Dec 1Jan 2Jan 3Jan 4Jan** >(Button)
此外,当用户可以移至往后日期或上一个日期时,我在两侧都有两个箭头 UIButton
。我有一个标签可以根据我显示的日期显示当前月份
这是我的完整代码:
Viewcontroller.h
@property (weak, nonatomic) IBOutlet UILabel *flabel;
@property (weak, nonatomic) IBOutlet UILabel *slabel;
@property (weak, nonatomic) IBOutlet UILabel *tlabel;
@property (weak, nonatomic) IBOutlet UILabel *folabel;
@property (weak, nonatomic) IBOutlet UILabel *fivlabel;
@property (weak, nonatomic) IBOutlet UILabel *sixlabel;
@property (weak, nonatomic) IBOutlet UILabel *sevenlabel;
Viewcontroller.m
#import "ViewController.h"
@interface ViewController (){
NSDate *firstdate;
}
@end
@implementation ViewController
@synthesize flabel;
@synthesize slabel;
@synthesize tlabel;
@synthesize folabel;
@synthesize fivlabel;
@synthesize sixlabel;
@synthesize sevenlabel;
@synthesize dateLabel;
@synthesize walletView;
@synthesize leftBtn;
@synthesize rightBtn;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// firstdate = [NSDate date];
// firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];
firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-5 toDate:[NSDate date] options:nil];
//
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
dateLabel.text = [dateFormat stringFromDate: firstdate];
[self dateChange];
}
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 7; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==6) {
dateFormatter.dateFormat=@"MMM,yyyy";
dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
{
leftBtn.enabled = false;
//It's the same day
}
else
{
leftBtn.enabled = true;
}
}
}
}
- (IBAction)calRight:(id)sender {
NSCalendar *calendar = [NSCalendar currentCalendar];
firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:7 toDate:firstdate options:nil];
[self dateChange];
//////
}
- (IBAction)calLeft:(id)sender {
NSCalendar *calendar = [NSCalendar currentCalendar];
firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:firstdate options:nil];
[self dateChange];
}
它已经适用于 6 个标签,但是当我添加 7 个标签时,我没有得到正确的日期。而且在这段代码中,当 las 标签有今天的日期时,我的左键将被禁用。
但我得到的结果是这样的:
Jan,2016 (label)
(button) < **30Dec 31Dec 1Jan 2Jan 3Jan 4Jan 17Dec** >(Button)
试试这样改:
在 ViewDidload 中:
firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:[NSDate date] options:nil];
// 下面的方法
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 7; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==6) {
dateFormatter.dateFormat=@"MMM,yyyy";
dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
{
leftBtn.enabled = false;
//It's the same day
}
else
{
leftBtn.enabled = true;
}
}
}
}
试试吧。让我知道它是否有效
我有一些 7 UILabels
会显示如下日期:
Jan,2016 (label)
(button) < **29Dec 30Dec 31Dec 1Jan 2Jan 3Jan 4Jan** >(Button)
此外,当用户可以移至往后日期或上一个日期时,我在两侧都有两个箭头 UIButton
。我有一个标签可以根据我显示的日期显示当前月份
这是我的完整代码:
Viewcontroller.h
@property (weak, nonatomic) IBOutlet UILabel *flabel;
@property (weak, nonatomic) IBOutlet UILabel *slabel;
@property (weak, nonatomic) IBOutlet UILabel *tlabel;
@property (weak, nonatomic) IBOutlet UILabel *folabel;
@property (weak, nonatomic) IBOutlet UILabel *fivlabel;
@property (weak, nonatomic) IBOutlet UILabel *sixlabel;
@property (weak, nonatomic) IBOutlet UILabel *sevenlabel;
Viewcontroller.m
#import "ViewController.h"
@interface ViewController (){
NSDate *firstdate;
}
@end
@implementation ViewController
@synthesize flabel;
@synthesize slabel;
@synthesize tlabel;
@synthesize folabel;
@synthesize fivlabel;
@synthesize sixlabel;
@synthesize sevenlabel;
@synthesize dateLabel;
@synthesize walletView;
@synthesize leftBtn;
@synthesize rightBtn;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// firstdate = [NSDate date];
// firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];
firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-5 toDate:[NSDate date] options:nil];
//
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
dateLabel.text = [dateFormat stringFromDate: firstdate];
[self dateChange];
}
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 7; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==6) {
dateFormatter.dateFormat=@"MMM,yyyy";
dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
{
leftBtn.enabled = false;
//It's the same day
}
else
{
leftBtn.enabled = true;
}
}
}
}
- (IBAction)calRight:(id)sender {
NSCalendar *calendar = [NSCalendar currentCalendar];
firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:7 toDate:firstdate options:nil];
[self dateChange];
//////
}
- (IBAction)calLeft:(id)sender {
NSCalendar *calendar = [NSCalendar currentCalendar];
firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:firstdate options:nil];
[self dateChange];
}
它已经适用于 6 个标签,但是当我添加 7 个标签时,我没有得到正确的日期。而且在这段代码中,当 las 标签有今天的日期时,我的左键将被禁用。
但我得到的结果是这样的:
Jan,2016 (label)
(button) < **30Dec 31Dec 1Jan 2Jan 3Jan 4Jan 17Dec** >(Button)
试试这样改:
在 ViewDidload 中:
firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:[NSDate date] options:nil];
// 下面的方法
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 7; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==6) {
dateFormatter.dateFormat=@"MMM,yyyy";
dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
{
leftBtn.enabled = false;
//It's the same day
}
else
{
leftBtn.enabled = true;
}
}
}
}
试试吧。让我知道它是否有效