标签: nsdatecomponents

设置NSDateComponents会导致NSDate不正确

我正在尝试获取一个21:00作为当地时间的NSDate对象 - 不关心哪一天.我对这个非常奇怪的结果感到头疼:

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setHour:21];
[components setMinute:0];
[components setSecond:0];

NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%@", date);
Run Code Online (Sandbox Code Playgroud)

结果是 0001-01-02 04:52:58 +0000

我不知道为什么.当前时间是太平洋标准时间17:34,但结果不会随当地时间而变化.

如果我调整setMinute和setSecond行

[components setMinute:7];
[components setSecond:2];
Run Code Online (Sandbox Code Playgroud)

我知道0001-01-02 05:00:00 +0000,这是正确的(太平洋标准时间21:00).

objective-c nsdate nsdatecomponents ios

4
推荐指数
1
解决办法
675
查看次数

尝试仅使用NSDate时间

我正在努力寻找一种方法来使用NSDate仅用于时间目的.我试图做以下代码:

- (void)awakeFromInsert
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.minute = 45;
    comps.second = 0;

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    self.periodDuration = [calendar dateFromComponents:comps];
    NSLog(@"periodDuration: %@",self.periodDuration);

    comps = [[NSDateComponents alloc] init];
    comps.hour = 8;
    comps.minute = 0;
    self.firstPeriodStartTime = [calendar dateFromComponents:comps];
    NSLog(@"firstPeriodTime: %@",self.periodDuration);
}
Run Code Online (Sandbox Code Playgroud)

但我得到的结果是:

periodDuration: 0001-12-31 22:24:04 +0000
firstPeriodTime: 0001-12-31 22:24:04 +0000
Run Code Online (Sandbox Code Playgroud)

结果我期待:

periodDuration: 45:00
firstPeriodTime: 08:00
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我怎样才能解决这个问题?谢谢.

xcode objective-c nsdate nsdatecomponents ios

4
推荐指数
1
解决办法
8232
查看次数

查找依赖于区域设置的第一天

给定一个NSDate,如何根据用户的区域设置找到该日期的第一天.例如,我听说有些国家将星期一视为一周的第一天,其他国家则使用星期日.我需要在第一种情况下返回前一个星期一,而在后一种情况下返回前一个星期日.

无论应用何种设备设置,我到目前为止的最佳努力始终返回前一个星期日:

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setLocale:[NSLocale currentLocale]];
NSDateComponents *components = [calendar components:NSYearForWeekOfYearCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit fromDate:originalDate];
[components setWeekday:1];
NSDate *firstDayOfWeek = [calendar dateFromComponents:components];
Run Code Online (Sandbox Code Playgroud)

奖金问题:在iOS上,哪个设置驱动这个?它是'地区格式'吗?

cocoa-touch objective-c nsdate nsdatecomponents ios

4
推荐指数
1
解决办法
1323
查看次数

迭代一年以获得Swift中每天的NSDate对象

您好我有一个方法,每天返回一个数组的时间.

prayTimesDate(date: NSDate, latitide : Double, longitude : Double, timeZone : Double) -> NSMutableArray
Run Code Online (Sandbox Code Playgroud)

我需要迭代一整年或者一个日期范围,以获得一年中每一天的一系列时间.我在ruby和python中找到了很多关于如何做到这一点的引用,但我找不到任何关于swift或objective-c的东西.swift中是否有任何内置方法可以实现此目的?如果没有,有人可以帮助我,因为我仍然是编程新手.任何输入都非常感谢.

这是我链接到我的swift项目的方法的objective-c代码

- (NSMutableArray *)prayerTimesDate:(NSDate *)date latitude:(double)latitude longitude:(double)longitude andTimezone:(double)timezone
{
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];

    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];

    return [self getDatePrayerTimesForYear:year month:month day:day latitude:latitude longitude:longitude andtimeZone:timezone];
}
Run Code Online (Sandbox Code Playgroud)

nsdate nsdatecomponents ios swift

4
推荐指数
1
解决办法
3980
查看次数

如何防止 DateComponents 更新时区

我正在尝试从 API 端点的日期值获取日期组件。我需要保留日期值,因为它们已经根据时区进行了调整。不幸的是,我无法控制 API 如何返回日期值。

当我从 API 获取日期值 ( sessionTime) 时,返回如下:

2017-12-05 08:00:00 +0000
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用以下代码从该日期对象中提取组件时,我需要根据该时间设置本地通知:

let notifyTime = Calendar.current.dateComponents(
     [.year, .month, .day, .hour, .minute, .second], from: sessionTime)
Run Code Online (Sandbox Code Playgroud)

我明白了:

year: 2017 month: 12 day: 5 hour: 2 minute: 0 second: 0 isLeapMonth: false 
Run Code Online (Sandbox Code Playgroud)

我距离格林威治标准时间有六个小时,所以很明显发生了什么,但我希望我可以阻止它并准确地提取日期组件。期望的输出是:

year: 2017 month: 12 day: 5 hour: 8 minute: 0 second: 0 isLeapMonth: false
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?谢谢!

date nsdatecomponents swift

4
推荐指数
1
解决办法
2333
查看次数

获取日期组件中的当前月份日期

我得到2018-06-01 00:00:00 +0000像这样的NSDateComponents 日期

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth|NSCalendarUnitYear |NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
 NSInteger month =[components month];
Run Code Online (Sandbox Code Playgroud)

当我打印 Components 的值时,我得到了这个值。

    TimeZone: GMT (GMT) offset 0
    Calendar Year: 2018
    Month: 5
    Leap month: no
    Day: 31
    Hour: 21
    Minute: 0
Run Code Online (Sandbox Code Playgroud)

我的预期输出应该是

    TimeZone: GMT (GMT) offset 0
    Calendar Year: 2018
    Month: 6
    Leap month: no
    Day: 1
    Hour: 0
    Minute: 0
Run Code Online (Sandbox Code Playgroud)

如何正确获取月份值?

objective-c nsdate nsdateformatter nsdatecomponents ios

4
推荐指数
1
解决办法
1922
查看次数

iPhone NSDateComponent错误日期1.1.2011?

我用'DateFromComponents'设置了一个日期,当我把它读出来......它的错误.我必须把DAY设置为第二个或之后的任何东西,所以我得到正确的年份?!?我设置2011年,当我读它我得到2010年!


 day = 1;
 month = 1;
 year = 2011;
 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 NSDateComponents *components = [[NSDateComponents alloc] init];
 [components setDay:day];
 [components setMonth:month];
 [components setYear:year];
 NSDate *date = [gregorian dateFromComponents:components]; 
 [gregorian release];


 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
 [dateFormatter setDateFormat:@"DD MMMM YYYY"];
 NSLog (@"hmm: %@",[dateFormatter stringFromDate:actDate]);
Run Code Online (Sandbox Code Playgroud)

-------我来了1 January 2010 /// 2010 not 2011!?!?

如何解决这个问题.

克里斯

iphone nsdatecomponents

3
推荐指数
1
解决办法
1315
查看次数

工作日的NSDateComponents没有显示正确的工作日?

我得到了一个N​​SDate例如1/6 -12(星期五)并试图找出它的工作日.我的周从星期一开始,所以星期五应该是工作日5.

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setFirstWeekday:2];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:firstOfJulyDate];
    NSLog(@"weekday %i",components.weekday);
Run Code Online (Sandbox Code Playgroud)

产出:工作日6

无论我设置setFirstWeekday到什么,输出将永远是6.我做错了什么?

objective-c nsdate nsdatecomponents ios ios5

3
推荐指数
2
解决办法
7179
查看次数

NSDateComponents不会正确更改小时组件

我无法理解为什么我无法正确更改NSDate的小时,使用NSDateComponents,我会这样做:

NSDateComponent components = [calendar components:  (NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit) fromDate:today];

[components setMinute:myMinute];
[components setHour:7];

NSDate *myNewDate = [calendar dateFromComponents:components];
NSLog(@"%@",myNewDate);
Run Code Online (Sandbox Code Playgroud)

但NSLog是这样的:

2012-12-08 06:00:00 +0000
Run Code Online (Sandbox Code Playgroud)

有一个小时的差异,我已经设置7但是有6个人可以帮助我吗?

iphone nsdate nsdatecomponents ios

3
推荐指数
1
解决办法
301
查看次数

如何在Objective c中获取当月的日期?

我正在开发一个使用日期,月份,年份的应用程序.我想要当前的月份日期,因为当前月份可以有28,29,30,31天.我试图获得当年的月份.但我不知道上面的代码.这是我的代码.

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate           *today           = [NSDate date];
NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
int currentYear  = [yearComponents year];
for(int months = 0; months < 12; months++)
{
    NSString *monthName = [NSString stringWithFormat:@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear];
    NSLog(@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear);
    [monthArray addObject:monthName];
}
Run Code Online (Sandbox Code Playgroud)

objective-c nsdate nsdatecomponents ios

3
推荐指数
2
解决办法
8804
查看次数