简体   繁体   English

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

[英]Get current month date in date components

I am getting NSDateComponents of date 2018-06-01 00:00:00 +0000 like this 我正在获取日期为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];

When I print the value of components , I get this value. 当我打印component的值时,我得到了这个值。

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

My expected out put should be 我的预期输出应该是

    TimeZone: GMT (GMT) offset 0
    Calendar Year: 2018
    Month: 6
    Leap month: no
    Day: 1
    Hour: 0
    Minute: 0

How can I get the value of month correctly? 如何正确获取月份的值?

When you use NSCalendar components:fromDate: the results are based on the calendar's current timezone which defaults to the user's local timezone. 当您使用NSCalendar components:fromDate:结果基于日历的当前时区,默认为用户的本地时区。

Your attempt to set the resulting components' timeZone doesn't alter the current components. 您尝试设置结果组件的timeZone不会更改当前组件。 That would only affect how the components would be interpreted if you used the components to create a new NSDate . 如果使用组件创建新的NSDate那只会影响组件的解释方式。

Assuming your goal is to get the components of date in UTC time and not in local time, then you need to set the calendar's timeZone before getting the components from date . 假设您的目标是以UTC时间而不是本地时间获取date的组成部分,那么您需要在从date获取组成部分之前设置日历的timeZone

NSDate *date = // your date
NSCalendar *calendar = NSCalendar.currentCalendar;
calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateComponents *components = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
NSLog(@"UTC Components: %@", components);

But keep in mind that you must understand which timezone you really want here. 但是请记住,您必须了解您真正想要的时区。 Be sure you really want the UTC timezone. 确保您确实想要UTC时区。 Don't use UTC just because it matches the output of NSLog(@"Date: %@", date); 不要仅仅因为它与NSLog(@"Date: %@", date);的输出匹配而使用UTC NSLog(@"Date: %@", date); . That log shows the date in UTC time. 该日志以UTC时间显示日期。

How do you create your initial date object? 您如何创建初始date对象? When I try that setup everything works as expected: 当我尝试安装时,一切都按预期工作:

NSString *dateString = @"2018-06-01 00:00:00 +0000";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

NSDate *date = [dateFormatter dateFromString:dateString];
NSCalendar *calendar = NSCalendar.currentCalendar;
NSDateComponents *components = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];

NSLog(@"Before: %@", components);
/*
 Before: <NSDateComponents: 0x604000158e10>
 Calendar Year: 2018
 Month: 6
 Leap month: no
 Day: 1
 Hour: 2
 Minute: 0
 */

components.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSLog(@"After: %@", components);
/*
 After: <NSDateComponents: 0x604000158e10>
 TimeZone: GMT (GMT) offset 0
 Calendar Year: 2018
 Month: 6
 Leap month: no
 Day: 1
 Hour: 2
 Minute: 0
 */

You need to get the last month date first. 您需要首先获取上个月的日期。

extension Date {

   //it will give the date of last month
   var previousMonthDate: Date {
      return Calendar.current.date(byAdding: .month, value: -1, to: self)!
   } 
}

Get the date components from date 从日期获取日期组件

let dt = Date()
let components = Calendar.current.dateComponents([.year, .month, .day], from: dt.previousMonthDate)
print(components.day ?? 0)
print(components.month ?? 0)
print(components.year ?? 0)

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

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