Number of days between two timestamps - ios

I want to get number of days between two timestamps but I am getting wrong value using this code.
Code :
let currentDateTimeStamp = Date().timeIntervalSince1970 * 1000.0
let firstDate = Date.init(timeIntervalSince1970: currentDateTimeStamp)
let lastDate = Date.init(timeIntervalSince1970: individualCellData["joining_date"] as! TimeInterval)
// First Method using extension
let daysBetween = firstDate.interval(ofComponent: .day, fromDate: lastDate)
// Second method
let components = Calendar.current.dateComponents([.day], from: lastDate, to: firstDate)
extension Date {
func interval(ofComponent comp: Calendar.Component, fromDate date: Date) -> Int {
let currentCalendar = Calendar.current
guard let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 }
guard let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 }
return end - start
}
}
I am getting timestamp from server in milliseconds. What is the correct way ?

let date1 = NSDate(timeIntervalSince1970: 1507211263)//Thursday, 5 October 2017 13:47:43
let date2 = NSDate(timeIntervalSince1970: 1507556863)//Monday, 9 October 2017 13:47:43
var secondsBetween: TimeInterval = date2.timeIntervalSince(date1 as Date)
var numberOfDays = Int(secondsBetween / 86400)
print("There are \(numberOfDays) days in between the two dates.")
//FYI: 86400 seconds = 24 hr

extension Date {
func timeStampToDay(timeStampInMillisecond:Double) -> Int {
let date = Date()
let todaysDateStamp = date.timeIntervalSince1970
let timeStampDate = Date(timeIntervalSince1970: timeStampInMillisecond / 1000)
var secBetween = Date(timeIntervalSince1970: todaysDateStamp).timeIntervalSince(timeStampDate)
return Int(abs(secBetween) / 86400)
}
func timeStampToDay(timeStampInSecond:Double) -> Int {
let date = Date()
let todaysDateStamp = date.timeIntervalSince1970
let timeStampDate = Date(timeIntervalSince1970: timeStampInMillisecond)
var secBetween = Date(timeIntervalSince1970: todaysDateStamp).timeIntervalSince(timeStampDate)
return Int(abs(secBetween) / 86400)
}
}

Related

IOS Count current streak of days

I'm making a day streak counter using UserDefaults and Core Data.
The idea is that a number will be added unto by 1 every separate day an action is performed-- this will be the streak number.
If this action wasn't performed for 24 hours, the number would reset to zero.
I have a function to set the end of the streak:
// set date time to the end of the day so the user has 24hrs to add to the streak
func changeDateTime(userDate: NSDate) -> NSDate {
let dateComponents = NSDateComponents()
let currentCalendar = NSCalendar.current
let year = Int(currentCalendar.component(NSCalendar.Unit.Year, fromDate:
userDate))
let month = Int(currentCalendar.component(NSCalendar.Unit.Month, fromDate:
userDate))
let day = Int(currentCalendar.component(NSCalendar.Unit.Day, fromDate: userDate))
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59
guard let returnDate = currentCalendar.dateFromComponents(dateComponents) else {
return userDate
}
return returnDate
}
It is returning the following Errors:
'NSDate' is not implicitly convertible to 'Date'; did you mean to use
'as' to explicitly convert?
Cannot convert value of type 'NSCalendar.Unit' to expected argument
type 'Calendar.Component'
When using the suggested corrections I only get more errors with no suggested corrections. I'm having trouble figuring out the proper way to express this
The full Code is:
let userDefaults = UserDefaults.standard
var moc: NSManagedObjectContext!
var lastStreakEndDate: NSDate!
var streakTotal: Int!
override func viewDidLoad() {
super.viewDidLoad()
// checks for object if nil creates one (used for first run)
if userDefaults.object(forKey: "lastStreakEndDate") == nil {
userDefaults.set(NSDate(), forKey: "lastStreakEndDate")
}
lastStreakEndDate = (userDefaults.object(forKey: "lastStreakEndDate") as! NSDate)
streakTotal = calculateStreak(lastDate: lastStreakEndDate)
}
// fetches dates since last streak
func fetchLatestDates(moc: NSManagedObjectContext, lastDate: NSDate) -> [NSDate] {
var dates = [NSDate]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "streakCount")
let datePredicate = NSPredicate(format: "date < %#", lastDate)
fetchRequest.predicate = datePredicate
do {
let result = try moc.fetch(fetchRequest)
let allDates = result as! [NSDate]
if allDates.count > 0 {
for date in allDates {
dates.append(date)
}
}
} catch {
fatalError()
}
return dates
}
// set date time to the end of the day so the user has 24hrs to add to the streak
func changeDateTime(userDate: NSDate) -> NSDate {
let dateComponents = NSDateComponents()
let currentCalendar = NSCalendar.current
let year = Int(currentCalendar.component(NSCalendar.Unit.Year, fromDate: userDate))
let month = Int(currentCalendar.component(NSCalendar.Unit.Month, fromDate: userDate))
let day = Int(currentCalendar.component(NSCalendar.Unit.Day, fromDate: userDate))
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59
guard let returnDate = currentCalendar.dateFromComponents(dateComponents) else {
return userDate
}
return returnDate
}
// adds a day to the date
func addDay(today: NSDate) -> NSDate {
let tomorrow = NSCalendar.currentCalendar.dateByAddingUnit(.Day, value: 1, toDate: today, options: NSCalendar.Options(rawValue: 0))
return tomorrow!
}
// this method returns the total of the streak and sets the ending date of the last streak
func calculateStreak(lastDate: NSDate) -> Int {
let dateList = fetchLatestDates(moc: moc, lastDate: lastDate)
let compareDate = changeDateTime(userDate: lastDate)
var streakDateList = [NSDate]()
var tomorrow = addDay(today: compareDate)
for date in dateList {
changeDateTime(userDate: date)
if date == tomorrow {
streakDateList.append(date)
}
tomorrow = addDay(today: tomorrow)
}
userDefaults.set(streakDateList.last, forKey: "lastStreakEndDate")
return streakDateList.count
}
Any Help is Appreciated
You need
// set date time to the end of the day so the user has 24hrs to add to the streak
func changeDateTime(userDate: Date) -> Date {
var dateComponents = DateComponents()
let currentCalendar = Calendar.current
let year = Int(currentCalendar.component(.year, from:
userDate))
let month = Int(currentCalendar.component(.month, from:
userDate))
let day = Int(currentCalendar.component(.day, from: userDate))
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59
guard let returnDate = currentCalendar.date(from:dateComponents) else {
return userDate
}
return returnDate
}
OR shortly
// set date time to the end of the day so the user has 24hrs to add to the streak
func changeDateTime(userDate: Date) -> Date {
var dateComponents = DateComponents()
let currentCalendar = Calendar.current
let res = currentCalendar.dateComponents([.year,.month,.day],from:userDate)
dateComponents.year = res.year
dateComponents.month = res.month
dateComponents.day = res.day
dateComponents.hour = 23
dateComponents.minute = 59
dateComponents.second = 59
guard let returnDate = currentCalendar.date(from:dateComponents) else {
return userDate
}
return returnDate
}

Swift - replacement of INT with Date and comparison

I have this code:
let miesiacOd : 2017
let rokOd : Int = 10
let dzienOd : Int = 1
let dataOd = String(format: "%02d-%02d-%02d", rokOd, miesiacOd, dzienOd)
let miesiacDo : Int = 2018
let rokDo : Int = 10
let dzienDo : Int = 1
let dataDo = String(format: "%02d-%02d-%02d", rokDo, miesiacDo, dzienDo)
let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "yyyy-MM-dd"
I'm trying to compare it, but I have error. When converting variables to dates:
let dataDo2 = dateFormatter2.date(from: dataDo)
let dataOd2 = dateFormatter2.date(from: dataOd)
I have the date and time as a result. For example: 2017-10-01 +000
Why is this happening and how to fix it?
Finally, I would like to check if the current date is within the above dates.
I'm trying to do it like this:
let sprawdzamDostepnoscDat = Date().isBetweeen(date: dataOd2!, andDate: dataDo2!)
extension Date {
func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
return date1.timeIntervalSince1970 < self.timeIntervalSince1970 && date2.timeIntervalSince1970 > self.timeIntervalSince1970
}
}
Will this solution be ok?
You don't need a formatter (string parser) to create Date:
var dateFromComponents = DateComponents()
dateFromComponents.year = 2017
dateFromComponents.month = 10
dateFromComponents.day = 1
let dateFrom = Calendar.current.date(from: dateFromComponents)
var dateToComponents = DateComponents()
dateToComponents.year = 2018
dateToComponents.month = 10
dateToComponents.day = 1
let dateTo = Calendar.current.date(from: dateToComponents)
Also note that Date is already comparable, therefore your inBetween function can be just:
extension Date {
func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
return date1 <= self && self <= date2
}
}
However, if you want to ignore time and just compare the days, you should use:
extension Date {
func isBetweeen(date date1: Date, andDate date2: Date) -> Bool {
return Calendar.current.compare(date1, to: self, toGranularity: .day) != .orderedDescending
&& Calendar.current.compare(self, to: date2, toGranularity: .day) != .orderedDescending
}
}

Creating a simple countdown to date in Swift 4

I'm working on a very simple app that counts down to a date. I found several tutorials but nothing in Swift 4. It seems like a lot has changed as I keep getting compiler errors.
Here is my code:
class ViewController: UIViewController {
#IBOutlet weak var CountdownText: UILabel!
let formatter = DateFormatter()
let userCalendar = NSCalendar.current
let requestedComponent: NSCalendar.Unit = [
NSCalendar.Unit.month,
NSCalendar.Unit.day,
NSCalendar.Unit.hour,
NSCalendar.Unit.minute,
NSCalendar.Unit.second,
]
func printTime()
{
formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
let startTime = NSDate()
let endTime = formatter.date(from: "12/03/18 2:00:00 p")
func timeDifference (requestedComponent: NSCalendar.Unit, from: startTime, to: endTime!, options: [NSCalendar.Options]) {}
CountdownText.text = "\(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds"
}
}
My errors are:
Use of undeclared type 'startTime'
Use of undeclared type 'endTime'
How to use
Copy the Code to your specific View Controller
Change the value of variable dateString with your date in the format
Date Format "< Month > < date >, < year > < hour >:< minute >:< second >"
Ex. "March 4, 2018 13:20:10"
Code
The below code will be useful for achieving a countdown timer of your custom date.
//
// DateCountDownTimer.swift
// CountDownTimerLearning
//
// Created by ThomasVEK on 04/03/18.
// Copyright © 2018 TVEK Solutions. All rights reserved.
//
import Foundation
func defaultUpdateActionHandler(string:String)->(){
}
func defaultCompletionActionHandler()->(){
}
public class DateCountDownTimer{
var countdownTimer: Timer!
var totalTime = 60
var dateString = "March 4, 2018 13:20:10" as String
var UpdateActionHandler:(String)->() = defaultUpdateActionHandler
var CompletionActionHandler:()->() = defaultCompletionActionHandler
public init(){
countdownTimer = Timer()
totalTime = 60
dateString = "March 4, 2018 13:20:10" as String
UpdateActionHandler = defaultUpdateActionHandler
CompletionActionHandler = defaultCompletionActionHandler
}
public func initializeTimer(pYear:Int, pMonth:String, pDay:Int, pHour:Int, pMin:Int, pSec:Int){
self.dateString = "\(pMonth) \(pDay), \(pYear) \(pHour):\(pMin):\(pSec)" as String
// Setting Today's Date
let currentDate = Date()
// Setting TargetDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm:ss"
dateFormatter.timeZone = NSTimeZone.local
let targedDate = dateFormatter.date(from: dateString) as! Date
// Calculating the difference of dates for timer
let calendar = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: targedDate)
let days = calendar.day!
let hours = calendar.hour!
let minutes = calendar.minute!
let seconds = calendar.second!
totalTime = hours * 60 * 60 + minutes * 60 + seconds
totalTime = days * 60 * 60 * 24 + totalTime
}
func numberOfDaysInMonth(month:Int) -> Int{
let dateComponents = DateComponents(year: 2015, month: 7)
let calendar = Calendar.current
let date = calendar.date(from: dateComponents)!
let range = calendar.range(of: .day, in: .month, for: date)!
let numDays = range.count
print(numDays)
return numDays
}
public func startTimer(pUpdateActionHandler:#escaping (String)->(),pCompletionActionHandler:#escaping ()->()) {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
self.CompletionActionHandler = pCompletionActionHandler
self.UpdateActionHandler = pUpdateActionHandler
}
#objc func updateTime() {
self.UpdateActionHandler(timeFormatted(totalTime))
if totalTime > 0 {
totalTime -= 1
} else {
endTimer()
}
}
func endTimer() {
self.CompletionActionHandler()
countdownTimer.invalidate()
}
func timeFormatted(_ totalSeconds: Int) -> String {
let seconds: Int = totalSeconds % 60
let minutes: Int = (totalSeconds / 60) % 60
let hours: Int = (totalSeconds / 60 / 60) % 24
let days: Int = (totalSeconds / 60 / 60 / 24)
return String(format: "%dD %02dH %02dM %02dS", days, hours, minutes, seconds)
}
}
You have specified timeDifference function inside printTime() function and in timeDifference() function you have defined from and to parameters which ones types are startTime and endTime which ones are not types. Replace them with NSDate like:
func timeDifference (requestedComponent: NSCalendar.Unit, from: NSDate, to: NSDate, options: [NSCalendar.Options]) {}
and then call this function with startTime and ednTime variables that you have defined.
Also I think that you should define timeDifference function outside of printTime function.

Swift Problems understanding the result of Calendar.dateComponents

in my app I have a list of contacts including the contacts birthday.
Now I want to see which of my contacts have birthday within in the next 7 days.
I am using a filter on my list to filter and return only thos contacts that match
let timeSpan = 7
let cal = Calendar.current
let now = Date()
var birthDays = contactList.filter { (contact) -> Bool in
if let birthDate = contact.birthDate {
let difference = cal.dateComponents([.day,.year], from: birthDate as Date, to: now! )
print("BD:\(birthDate) : DIFF \(difference.day!)")
return ((difference.day! <= timeSpan) && difference.day! >= 0)
}
return false
}
My hope was so. However the result was weired. So I added that ugly print into my closure in order to see the result of 'difference'
What is odd is that for instance the following:
Today is 2017-08-18 one of my contacts was born on 1987-08-19.
So instead of returning a difference.day of 1 I receive 364. If I swap from: and to: in the dateComponents I receive difference.day of -364.
My expectation was to have a difference.day = 1.
Again in Playground
import UIKit
var now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.date(from: "1987-08-19")
let cal = Calendar.current
let difference = cal.dateComponents([Calendar.Component.day,Calendar.Component.month,Calendar.Component.year], from: s!, to: now )
print("\(difference.day!)") // result is difference.day = 30
What am I doing wrong?
Thanks
You can create an extension to return the number of days from the next birthday as follow:
extension Date {
var year: Int { return Calendar.current.component(.year, from: self) }
var month: Int { return Calendar.current.component(.month, from: self) }
var day: Int { return Calendar.current.component(.day, from: self) }
var noon: Date { return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)! }
var daysFromBirthday: Int {
let nextBirthDate = DateComponents(calendar: .current, year: Date().year + (month < Date().month ? 1 : 0), month: month, day: day, hour: 12).date ?? Date.distantFuture
return Calendar.current.dateComponents([.day], from: Date().noon, to: nextBirthDate).day ?? 0
}
}
And you can now filter your objects as follow:
let timeSpan = 0...7
let birthDays = contactList.filter {
timeSpan ~= $0.birthDate?.daysFromBirthday ?? -1
}
The problem was you were checking the difference of the days from the user's birthday but in the app we will need to get the difference between the current date and the birthday during this year. Say in the example that you have said, it's the difference between 2017-08-19(this year's birthday) and 2017-08-18(current date). You could try the code below.
var now = Date()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.date(from: "1987-08-19")
let cal = Calendar.current
let currentDateComponentsYear = cal.dateComponents([.year,.month,.day], from: newDate!)
var dateComponents = cal.dateComponents([.year,.month,.day], from: s!)
dateComponents.year = currentDateComponentsYear.year
let currentYearBDay = cal.date(from: dateComponents)
currentDateComponentsYear.month
let difference = cal.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from:newDate! , to: currentYearBDay! )
let daysDiffernce:Int?
if dateComponents.month == 1 && currentDateComponentsYear.month == 12 && difference.day! < 0 {
let range = cal.range(of: .day, in: .month, for: newDate!)!
let numDays = range.count
daysDiffernce = difference.day! + numDays
} else {
daysDiffernce = difference.day
}
print("\(daysDiffernce!)") //result 1

How to get week start date and end date by using any Month and week number swift 3?

I have to implement graph so that I need to get week start date and weekend date if I will pass the date object and week number.
How can I achieve that I tried it but didn't get exactly?
Here below is my code:-
Weekday:-
//Day of week
func getDayOfWeek(today:String)->Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let todayDate = formatter.date(from: today) {
let myCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
let myComponents = myCalendar.components(.weekday, from: todayDate)
let weekDay = myComponents.weekday
return weekDay
} else {
return nil
}
}.
extension Date {
var millisecondsSince1970:Int {
return Int((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds:Int) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
}
func startOfWeek(weekday: Int?) -> Date {
var cal = Calendar.current
var component = cal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
component.to12am()
cal.firstWeekday = weekday ?? 1
return cal.date(from: component)!
}
func endOfWeek(weekday: Int) -> Date {
let cal = Calendar.current
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
component.to12pm()
return cal.date(byAdding: component, to: startOfWeek(weekday: weekday))!
}
}
internal extension DateComponents {
mutating func to12am() {
self.hour = 0
self.minute = 0
self.second = 0
}
mutating func to12pm(){
self.hour = 23
self.minute = 59
self.second = 59
}
}
This returns start- and end date for a given week number and date
func dayRangeOf(weekOfYear: Int, for date: Date) -> Range<Date>
{
let calendar = Calendar.current
let year = calendar.component(.yearForWeekOfYear, from: date)
let startComponents = DateComponents(weekOfYear: weekOfYear, yearForWeekOfYear: year)
let startDate = calendar.date(from: startComponents)!
let endComponents = DateComponents(day:7, second: -1)
let endDate = calendar.date(byAdding: endComponents, to: startDate)!
return startDate..<endDate
}
print(dayRangeOf(weekOfYear: 12, for: Date()))
Consider that print displays the dates in UTC and the start date depends on the first weekday setting of the current locale.
Edit
A version to determine the range of a given week of month
func dayRangeOf(weekOfMonth: Int, year: Int, month: Int) -> Range<Date>? {
let calendar = Calendar.current
guard let startOfMonth = calendar.date(from: DateComponents(year:year, month:month)) else { return nil }
var startDate = Date()
if weekOfMonth == 1 {
var interval = TimeInterval()
guard calendar.dateInterval(of: .weekOfMonth, start: &startDate, interval: &interval, for: startOfMonth) else { return nil }
} else {
let nextComponents = DateComponents(year: year, month: month, weekOfMonth: weekOfMonth)
guard let weekStartDate = calendar.nextDate(after: startOfMonth, matching: nextComponents, matchingPolicy: .nextTime) else {
return nil
}
startDate = weekStartDate
}
let endComponents = DateComponents(day:7, second: -1)
let endDate = calendar.date(byAdding: endComponents, to: startDate)!
return startDate..<endDate
}
print(dayRangeOf(weekOfMonth: 5, year: 2017, month: 6))
The result type of the second version is an optional because there are a few calculations which could fail for example if the number of week in the particular month is out of range.
For anyone interested in this, it looks like OP confusing weekOfMonth and weekOfYear…
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
let cal = Calendar.current
let dateComponents = DateComponents(year: 2018, month: 3, day: 15)
let date = cal.date(from: dateComponents)!
func weekOfMonthStart(forDate date: Date) -> Date {
var compsToWeekOfMonth = cal.dateComponents([.year, .month, .weekOfYear], from: date)
compsToWeekOfMonth.day = cal.range(of: .day, in: .weekOfMonth, for: date)?.lowerBound
return cal.date(from: compsToWeekOfMonth)!
}
Somebody mention an answer that will fail, so a test was included ;)
for i in 0...5000 {
let newDate = cal.date(byAdding: DateComponents(day:i), to: date)!
weekOfMonthStart(forDate: newDate)
}

Resources