Inconsistency with Calendar.dateComponents in swift

2

I have the variable fecha that when doing print shows per console: 2016-11-20 23:00:00 +0000

The problem is that when you do this:

let auxCalendar = calendar.dateComponents([.weekday, .day, .weekdayOrdinal, .month], from: fecha)
print("*\(fecha)*")
print("\n***\n\(auxCalendar)\n\(fecha)\n***\n")

Show me this:

  

* 2016-11-20 23:00:00 +0000 *

     

***
  month: 11 day: 21 weekday: 2 weekdayOrdinal: 3 isLeapMonth: false
  2016-11-20 23:00:00 +0000   
***

As you can see there is an incongruity, because I have passed the date with day 20 and he returns me on the 21st day

The variable calendar is initialized like this:

var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "es_ES")
    
asked by 21.11.2016 в 11:21
source

1 answer

1

You need to declare the time zone of your geographic region.

var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "es_MX")
calendar.timeZone = TimeZone(identifier: "America/Mexico_City")
    
answered by 14.02.2017 в 00:58