繁体   English   中英

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

[英]How to get calendar date of today and move towards

我知道如何使用标签来显示今天的月份和年份。我使用此代码。

// 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];

上面的代码将显示带有月份的当前月份。 但是我需要的是。 我需要做下图

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

因此, todays date is 29th Dec ,它应该start with 29Dec todays date is 29th Dec start with 29Dec并应显示接下来的6 days date 。就像用户在tomorrowon 30th dec打开一样,明智的做法。 我需要显示如下图所示:

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

就像明智的做法一样,当用户在31 Dec打开我的应用程序时,它应显示如下。

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

因此,当用户打开我的应用程序时,应仅将当前currrent datefirst并在next 6 days date连续使用。

请帮我怎么做。我是ios新手。 您的帮助对我了解更多非常有用。

编辑:

#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];
    }


}

该代码以ddMMM格式创建今天和之后6天的日期。 将标签放入数组中,然后按索引将日期分配给标签。

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];
}

但是,它不考虑潜在的夏时制更改问题。 当前日期应调整为安全的时间,不会出现任何问题。

看看NSCalendar和有关日历计算的部分。

您需要获取当前日历。

从开始日期开始加载月/日/年组件。 (今天?)

将日期组件的小时值设置为12(以避免日期从夏时制变为标准时间,添加leap秒的天数等问题)

使用dateFromComponents可以在相关日期的中午获取NSDate。 我们称其为noonDate。

使用'dateByAddingUnit:value:toDate:options:'将一天添加到noonDate:

将dateByAddingUnit:value:toDate:options:的结果提供给日期格式化程序,以所需的显示格式的本地化版本进行显示。

您没有标记问题Objective-C,所以我编写了一个快速代码,因为使用游乐场非常方便。

为了避免DST和类似的陷阱,我使用NSDateComponents进行计算。

该代码将导致包含标签的数组。

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
}

由于您知道如何从NSDate中获取所需的信息,因此只需要将该日期增加1天即可。

使用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