简体   繁体   中英

How to get calendar date of today and move towards

I know how to display today month with year using label .i use this code.

// show the present month
    NSDate *date = [NSDate date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat=@"MMMM";
    NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
    dateFormatter.dateFormat=@"yyyy";
    NSString * yearString = [[dateFormatter stringFromDate:date] capitalizedString];
    dateLabel.text = [NSString stringWithFormat:@"%@ ,%@",monthString,yearString];

This above code will display the Present month with year. But what i need is. I need to do like below image

1.. 在此处输入图片说明

So todays date is 29th Dec and it should start with 29Dec and should display next 6 days date .Like wise when user open on tomorrow like say on 30th dec . I need to disply like below image:

2.. 在此处输入图片说明

Like wise when user open my app on 31 Dec it should display like this.

3.. 在此处输入图片说明

So when ever user open my app that currrent date alone should be in first with continuous on next 6 days date .

Please help me how to do that.I am new to ios. Your help will be very usefull for me to know more.Thanks

EDITED:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize flabel;
@synthesize slabel;
@synthesize tlabel;
@synthesize folabel;
@synthesize fivlabel;
@synthesize sixlabel;
@synthesize sevenlabel;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel, sevenlabel ];
    NSDate *today = [NSDate date];
    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:today options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];
    }


}

This code creates the date in format ddMMM for today and the next 6 following days. Put the labels in an array and assign the dates to the labels by index.

NSArray *labelArray = @[firstDateLabel, secondDateLabel ...   ];

NSDate *today = [NSDate date];
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:today options:nil];
  UILabel *label = (UILabel *)labelArray[i];
  label.text = [dateFormatter stringFromDate:nextDate];
}

However it doesn't consider potential daylight saving changing problems. The current date should be adjusted to a safe time where no problems can occur.

Take a look at NSCalendar and the section on calendrical calculations.

You need to fetch the current calendar.

Load the month/day/year components from your starting date. (Today?)

Set the hours value of your date components to 12 (to avoid problems with dates that change from daylight savings time to standard time, days when leap seconds are added, etc.)

Use dateFromComponents to get an NSDate for noon on the day in question. Let's call that noonDate.

Use 'dateByAddingUnit:value:toDate:options:' to add a day to noonDate:

Feed the result of dateByAddingUnit:value:toDate:options: to your date formatter to display it in localized version of your desired display format.

you didn't tag your question objective-c, so I wrote a swift code, as it is so much more convenient to use playgrounds.

To avoid DST and similar pitfalls, I am using NSDateComponents for the calculation.

The code results in a array containing the labels.

import UIKit
let cal = NSCalendar.currentCalendar()
let now = NSDate()

var dates = [NSDate]()
for i in 0..<7 {
    let comps = cal.components([.Day, .Month, .Year], fromDate: now)
    comps.day += i
    dates.append(cal.dateFromComponents(comps)!)
}

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dMMM"
let labels = dates.map { (date) -> UILabel in
    let dateSting = dateFormatter.stringFromDate(date)
    let l = UILabel()
    l.backgroundColor = UIColor.whiteColor()
    l.textColor = UIColor.blackColor()
    l.text = dateSting
    l.sizeToFit()
    return l
}

Since you know how to get required information out of NSDate, you only need to increase that date by 1 day.

That can be done easily with dateByAddingTimeInterval :

// 86400 is the number of seconds in 1 day
NSDate *nextDay=[currentDay dateByAddingTimeInterval:86400];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM