- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这里我在顶部使用了一个标签来显示今天的月份和年份。我还有 6 个标签,可以显示今天的日期,比如 12 月 31 日到 1 月 5 日。这是代码:
这将显示从接下来的 5 个日期开始的今天
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
}
这将显示当前月份和年份
// show the present month
NSDate *date = [NSDate date];
// NSDateFormatter *date = [[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];
重要:
我需要的是如下图所示:
当前日期应该显示在最后并且应该显示之前的 5 个日期。
我在左边和右边有两个按钮。每当用户按下右键时,就像说现在是 12 月 31 日。当用户按右时,它应该更改为 1Jan 并且应该显示从 1 月 1 日开始的前 5 个日期。而且月份 .label 应该自动更改为 2016 年 1 月。如下图:
两个按钮操作:
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
这段代码是我尝试更改的一个月才完成的。但没有改变我的日期标签。
请用代码解释一下。
完整编辑:
- (void)viewDidLoad {
[super viewDidLoad];
NSDate *firstLabelDate = [NSDate date]; //make it class variable
NSDate *lastLabelDate = [NSDate date]; //make it class variable
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate];
}
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?1:-1;
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
UILabel *label = (UILabel *)self.labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
最佳答案
全局定义一个 Date 对象 NSDate *firstdate;
并在 viewDidLoad firstdate = [NSDate date]; 中赋值
现在使用以下方法显示您的日期和月份从 viewDidLoad [self dateChange];
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==5) {
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateFormatter.dateFormat=@"yyyy";
NSString * yearString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateLabel.text = [NSString stringWithFormat:@"%@ ,%@",monthString,yearString];
}
}
}
现在将您的 calRight 和 calLeft 设置为如下所示
- (IBAction)calRight:(id)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}
- (IBAction)calLeft:(id)sender {
firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
[self dateChange];
}
关于ios - 显示日历动态还显示月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34542105/
我正在用 javascript 制作日历,我不想在 中显示当前日期、日期名称和月份名称它在我的函数内 Kalender()但由于某种原因,如果我执行函数 volgende() (下个月)它更改 中
有一个输入标签应用了数据掩码。 这允许输入数据,例如 02/12/2014 或 13/05/2014。但不接受 1/1/2013 或 13/5/2014。请帮忙!!! 最佳答案 根据jQuery
我正在尝试创建一个函数,它将接受三个输入变量(开始日期、结束日期、粒度),并将输出一个包含所有日期的数组。输出将根据输入以每日或每月为粒度。例如,如果我查看 2015 年 1 月 - 2015 年 2
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有一个事件表,其中 X 为频率(每 X 个月一次),第一个开始日期和结束日期如下: 如何复制每一行并将其粘贴到新工作表中,并根据 X 和月份日期的增量为每一行添加附加行,如下所示: 最佳答案 这里是
我将以下格式的日期存储在Mailed_Date列中 Mon, 09/20/10 01:04 PM 我使用了一个serde(csv-serde-1.1.2-0.11.0-all.jar)从csv文件中获
我想根据日期和月份值对数组进行排序。我正在使用此代码 NSSortDescriptor *descriptor=[[NSSortDescriptor alloc]initWithKey:@"self
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
我无法获得正确的查询结果 我正在查询获取在相应月份注册了多少用户。到目前为止,我提出了以下查询,它允许我获得该结果,但没有显示没有用户注册的月份 SELECT YEAR(c.created_at) a
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
在表中,我有一列名为“service_time”的列,它是服务的开始日期,另一列“times_year”是每年应完成服务的次数。 我遇到的问题是如何在 -> (thisMonth == service
使用:Python 3.6, Pandas 0.22 我有一个 .csv 文件,我需要从中获取基于月份和位置的平均值。这是数据中的一行,还有更多具有多个位置和日期的行: 名称日期雪 大急流城杰拉尔德福
我想循环遍历给定开始时间以来的月份,并打印第一天和最后一天。我可以手动跟踪它是哪个月份和年份,并使用 calendar.monthrange(year, Month) 来获取天数......但这是最好
我正在做试卷的最后一题,但我已经迷失在算法的创建过程中。我的定义图看起来不错,但我就是无法确定计算月份方面的顺序。 题目如下: A file of transaction records includ
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我正在使用以下格式来格式化 DateTime: DateTime CusDate = dateTimePicker1.Value; string Date = CusDate.ToString("dd
废话不多少,上代码 复制代码 代码如下: // 获取指定日期所在星期的开始时间与结束时间 function getWeekRange($date){ &nb
今天我运行了自动化测试,并想知道为什么我收到了有关某些特定日期内容的错误。 事实证明,设置固定的 UTC 月份不再有效。但昨天确实如此。据我所知,没有任何变化。 我尝试运行以下代码 var d = n
我有一个包含两个日期字段的 Grails 域 Date updated Date created 我想根据月、小时或年的更新时间来计算行数。我怎样才能做到这一点。互联网上显示的方法不起作用。我正在使用
是否有任何内置函数可用于数据框对象以生成类 Date 时间序列上的变量以创建星期几、月份、年份、年份中的星期,等在 R 中? 基础包中的weekdays、months、quarters函数生成文本输出
我是一名优秀的程序员,十分优秀!