简体   繁体   English

如何获取今天的日历日期并走向

[英]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.. 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 . 因此, todays date is 29th Dec ,它应该start with 29Dec todays date is 29th Dec start with 29Dec并应显示接下来的6 days date 。就像用户在tomorrowon 30th dec打开一样,明智的做法。 I need to disply like below image: 我需要显示如下图所示:

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

Like wise when user open my app on 31 Dec it should display like this. 就像明智的做法一样,当用户在31 Dec打开我的应用程序时,它应显示如下。

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

So when ever user open my app that currrent date alone should be in first with continuous on next 6 days date . 因此,当用户打开我的应用程序时,应仅将当前currrent datefirst并在next 6 days date连续使用。

Please help me how to do that.I am new to ios. 请帮我怎么做。我是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. 该代码以ddMMM格式创建今天和之后6天的日期。 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. 看看NSCalendar和有关日历计算的部分。

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.) 将日期组件的小时值设置为12(以避免日期从夏时制变为标准时间,添加leap秒的天数等问题)

Use dateFromComponents to get an NSDate for noon on the day in question. 使用dateFromComponents可以在相关日期的中午获取NSDate。 Let's call that noonDate. 我们称其为noonDate。

Use 'dateByAddingUnit:value:toDate:options:' to add a day to noonDate: 使用'dateByAddingUnit:value:toDate:options:'将一天添加到noonDate:

Feed the result of dateByAddingUnit:value:toDate:options: to your date formatter to display it in localized version of your desired display format. 将dateByAddingUnit:value:toDate:options:的结果提供给日期格式化程序,以所需的显示格式的本地化版本进行显示。

you didn't tag your question objective-c, so I wrote a swift code, as it is so much more convenient to use playgrounds. 您没有标记问题Objective-C,所以我编写了一个快速代码,因为使用游乐场非常方便。

To avoid DST and similar pitfalls, I am using NSDateComponents for the calculation. 为了避免DST和类似的陷阱,我使用NSDateComponents进行计算。

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. 由于您知道如何从NSDate中获取所需的信息,因此只需要将该日期增加1天即可。

That can be done easily with dateByAddingTimeInterval : 使用dateByAddingTimeInterval可以轻松实现

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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