簡體   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