Calculating StartDate and endDate of a day based on todays date. Swift 2 - swift

I have the following function which i am using to calculate a start and end of todays date. It was working perfectly last week, i think it may have something to do with the clocks going forwards?
func rangeOfPeriod(period: NSCalendarUnit, date: NSDate) -> (NSDate, NSDate) {
let calendar = NSCalendar.currentCalendar()
var startDate: NSDate? = nil
var duration: NSTimeInterval = 0
calendar.rangeOfUnit(period, startDate: &startDate, interval: &duration, forDate: date)
let endDate = startDate!.dateByAddingTimeInterval(duration - 1)
return (startDate!, endDate)
}
When I try and calculate the start and end of a day using:
print("Start of Day = \(rangeOfPeriod(.Day, date: NSDate()).0), End of Day = \(rangeOfPeriod(.Day, date: NSDate()).1)")
I get the following, where the start and end date is 1hour behind what is expected:
Start of Day = 2016-03-27 23:00:00 +0000, End of Day = 2016-03-28 22:59:59 +0000
when i would expect:
Start of Day = 2016-03-28 00:00:00 +0000, End of Day = 2016-03-28 23:59:59 +0000
Any help would be appreciated

Related

Swift date components incorrect return of date day number

I need to obtain a date from some variable values
So I specify year, month and day and I need a Date as return
Doing the following works except for the day because it return the day input - 1
let todayDate: Date = Calendar.current.startOfDay(for: Date.from(year: 2022, month: 09, day: 05)!)
print("today date = \(todayDate)")
extension Date {
static func from(year: Int, month: Int, day: Int) -> Date? {
let calendar = Calendar(identifier: .gregorian)
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
return calendar.date(from: dateComponents) ?? nil
}
}
And the output is
today date = 2022-09-04 22:00:00 +0000
Date and time can be a bit tricky. The Date struct stores a point in time relative to GMT. If you print it it will show exactly that.
Solution:
DonĀ“t use print, use a proper Dateformatter. To illustrate what I mean use this in a playground:
let date = Calendar.current.startOfDay(for: Date())
print(date)
//2022-09-03 22:00:00 +0000
// when it is 4.th of september 00:00 in my timezone (+- Daylight saving) it is this time in GMT
let formatter = DateFormatter()
formatter.dateFormat = "dd MM yyyy HH:mm:ss"
print(formatter.string(from: date))
//04 09 2022 00:00:00
// this is the time in my timezone
So the issue here is not that it has the wrong time, it is just not presented in the correct time zone.

Problem with adding month value to date in Swift

I've got a problem. I'm getting start date of current year with function below:
func startOfYear() -> Date {
var calendar = Calendar(identifier: .iso8601)
let timezone = TimeZone(secondsFromGMT: 0)!
calendar.timeZone = timezone
return calendar.dateComponents([.calendar, .year], from: self).date!
}
My goal is to iterate through this date to extract data for every single month. I use this function to add value to .month to get start date of every month of year:
func getNextMonths(using calendar: Calendar = .iso8601, monthAmount: Int) -> Date {
calendar.date(byAdding: .month, value: monthAmount, to: self.startOfYear())!
}
When I iterate through this fuction and print dates I receive this:
func getMonth() {
for number in 0...12 {
print("month \(number): \(Date().getNextMonths(monthAmount: number))")
}
}
output:
month 0: 2022-01-01 00:00:00 +0000
month 1: 2022-02-01 00:00:00 +0000
month 2: 2022-03-01 00:00:00 +0000
month 3: 2022-03-31 23:00:00 +0000
month 4: 2022-04-30 23:00:00 +0000
month 5: 2022-05-31 23:00:00 +0000
month 6: 2022-06-30 23:00:00 +0000
month 7: 2022-07-31 23:00:00 +0000
month 8: 2022-08-31 23:00:00 +0000
month 9: 2022-09-30 23:00:00 +0000
month 10: 2022-11-01 00:00:00 +0000
month 11: 2022-12-01 00:00:00 +0000
month 12: 2023-01-01 00:00:00 +0000
As you can see there is a problem starting with 2022-03-31 - it supposed to return 2022-04-01 but I don't really know what I'm doing wrong and why I can't achieve this.
EDIT
I've changed fuction getNextMonths to check if it's daylight saving time and it's working now, thanks to [Joakim Danielson][1] for help.
It's silly solution but it works:
func getNextMonths(using calendar: Calendar = .iso8601, monthAmount: Int) -> Date {
if monthAmount == 0 || monthAmount == 1 || monthAmount == 2 || monthAmount == 10 || monthAmount == 11 || monthAmount == 12 {
return calendar.date(byAdding: .month, value: monthAmount, to: self.startOfYear())!
} else {
var dateComponents = DateComponents()
dateComponents.month = monthAmount
dateComponents.hour = 1
return calendar.date(byAdding: dateComponents, to: self.startOfYear())!
}
}
Hey I copied your code as an extension to Date and I got some errors, after some search I finally can run it and it gives me the beginning of each month, here's my changes hope it helps!
extension Date{
func startOfYear() -> Date {
var calendar = Calendar(identifier: .iso8601)
let timezone = TimeZone(secondsFromGMT: 0)!
calendar.timeZone = timezone
return calendar.dateComponents([.calendar, .year], from: self).date!
}
func getNextMonths(using calendar: Calendar, monthAmount: Int) -> Date {
calendar.date(byAdding: .month, value: monthAmount, to: self.startOfYear())!
}
func getMonth() {
for number in 0...12 {
print("month \(number): \(Date().getNextMonths(using: Calendar.current, monthAmount: number))")
}
}
}
Then I made a Date object like this
let d = Date()
print(d.getMonth())
Output (In Indian numbers, but it should be the same)

GMT to local time conversion ERROR with swift UIKIT

Does anyone have an answer to why I am getting -8 hours instead of -7 (my timezone) hours difference upon converting time from GMT to local i using the formula below:
print("TIMEZONE IN HOURS: \(timeZone/3600)")
let interval = Date(timeIntervalSinceReferenceDate: 93866.4142533315)
print("GMT TIME: \(interval)")
let intervalDateComponents = calendar.dateComponents([.hour, .minute, .second], from: interval)
let mHour = intervalDateComponents.hour ?? 0
let mMinute = intervalDateComponents.minute ?? 0
print("INTERVAL DATE COMPONENT: \(intervalDateComponents)")
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let intervalDateString = formatter.string(from: interval)
print("DATE STRING FORMAT: \(intervalDateString)")
prints out the following: (please pay attention to date)
TIMEZONE IN HOURS: -7
GMT TIME: 2001-01-02 02:04:26 +0000 (January 2)
INTERVAL DATE COMPONENT: hour: 18 minute: 4 second: 26 isLeapMonth: false
DATE STRING FORMAT: 2001-01-01 18:04:26 (January 1)
interval HOURS: 18
edit/update:
let mDateString = formatter.string(from: interval)
print("DATE STRING FORMAT: (mDateString)")
let mdate = formatter.date(from: mDateString)
print("MDATE: (mdate)")
let mDateComponents = calendar.dateComponents([.hour, .minute, .second], from: mdate!)
print("M DATE COMPONENT: (mDateComponents)")
prints out:
DATE STRING FORMAT: 2001-01-01 19:01:27 -0700
MDATE: Optional(2001-01-02 02:01:27 +0000)
M DATE COMPONENT: hour: 18 minute: 1 second: 27 isLeapMonth: false
If you notice above in the code my intention is to extract the local time component to trigger alarm at a later time daily. So I fed the string that shows
2001-01-01 19:01:27 -0700
I converted the string to mdate in order to get time components that I need to use for the alarm to work on time so I got (hour: 18) rather than hour: 19 which I used to generate the mdate string.
The problem there is that you are getting the timezone offset for today. You need to get the timezone offset for the same date you are using:
let timeZone = TimeZone.current.secondsFromGMT(for: interval)
print("TIMEZONE IN HOURS: \(timeZone/3600)")

While a date is not within 2 dates, increase the 2 dates until the date is within. Swift

I have the following code which has a function with a selected date which is chosen by a user. This passes in a currentStart and endDate which is retrieved from CoreData and i want to create a newStartDate and newEndDate based on the previous startDate and endDate which was passed into the below function:
func calcStartAndEndDateBasedOnCurrentExsisting(selectedSpendDate: NSDate, currentStartDate: NSDate,
currentEndDate: NSDate, durationOccurance: Double) {
//initialising the newStartDate and newEndDate
var newStartDate : NSDate = currentStartDate
var newEndDate : NSDate = currentEndDate
var occurance : Int
//if the selectedSpendDate is before the currentStartDate, make the occurance a negative so it goes back in time. durationOccurance is how many days to increase/decrease by
if selectedSpendDate.earlierDate(currentStartDate).isEqualToDate(selectedSpendDate) {
occurance = -Int(durationOccurance)
}
else {
occurrence = Int(durationOccurance)
}
print("old end date = \(currentEndDate)")
components.day = occurance
while newStartDate.compare(selectedSpendDate) == .OrderedDescending || newEndDate.compare(selectedSpendDate) == .OrderedAscending {
print("\(selectedSpendDate) is not within \(newStartDate) & \(newEndDate)")
let test = newStartDate
print("old start date = \(test)")
let newDate = calendar.dateByAddingComponents(components, toDate: test, options: [])!
newStartDate = newDate
print("new StartDate = \(newStartDate)")
//working out end date from the computed start date
let calculatedEndDay = rangeOfPeriod(.Day, date: newStartDate).1
components.day = Int(durationOccurance) - 1
newEndDate = calendar.dateByAddingComponents(components, toDate: calculatedEndDay, options: [])!
print("Start: \(newStartDate) End: \(newEndDate)")
}
}
It seems to work for initially. For example when the currentStartDate is 24/03/2016, this changes successfully to the 25/03/2016 however this never changes to the 26/03/2016.
the output is shown below:
old end date = 2016-03-24 23:59:59 +0000
2016-03-26 10:20:22 +0000 is not within 2016-03-24 00:00:00 +0000 & 2016-03-24 23:59:59 +0000
old start date = 2016-03-24 00:00:00 +0000
new StartDate = 2016-03-25 00:00:00 +0000
Start: 2016-03-25 00:00:00 +0000 End: 2016-03-25 23:59:59 +0000
2016-03-26 10:20:22 +0000 is not within 2016-03-25 00:00:00 +0000 & 2016-03-25 23:59:59 +0000
old start date = 2016-03-25 00:00:00 +0000
new StartDate = 2016-03-25 00:00:00 +0000
Any help would be appreciated :)
It works as expected when components.day = occurance is within the while loop

Timestamp to weekday going wrong

I made this function:
func getDayOfWeek(date: NSDate) -> String? {
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let myComponents = myCalendar?.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date)
let weekDay = myComponents?.weekday
println(date)
return weekNumberToWord(weekDay!)
}
To which I give a timestamp as date (1439460000, which is the date of today) and it prints: 2046-08-13 10:00:00 +0000. How is this possible?
This is how I call the function:
var day = getDayOfWeek(NSDate(timeIntervalSinceReferenceDate: timestamp.doubleValue))!
In which getDayOfWeek() is a switch converting all the numbers to day name as string.
You are using the wrong initializer:
init(timeIntervalSinceReferenceDate:)
Returns an NSDate object initialized relative the first instant of 1 January 2001, GMT by a given number of seconds.
You should use init(timeIntervalSince1970 seconds: NSTimeInterval) to initialize a date from a UNIX timestamp (which you are apparently using):
Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.
var day = getDayOfWeek(NSDate(timeIntervalSince1970: timestamp.doubleValue))!