Swift NSDate create var with hour and minute only - swift

I asked a question a moment ago about how to get the hour and minute of the current time and not worry about the year month day, and that question was solved. Im now trying to apply the same logic to creating a custom time (in the hour minute format) but im getting an error
i have
let time1 = NSDateComponents()
time1.hour = 8
time1.minute = 54
let date1: NSDate = calendar.dateFromComponents(time1)!
I tried using the previous answer to make it look like
let date1: NSDate = calendar.components([ .Hour, .Minute ], fromDate: time1())
but i get "ambiguous reference to member 'components'"
from there i remove the : NSDate...then im told to remove the () from the end....and i keep getting errors. Is there an easy fix to this?
Thanks

The quick way, getting the nsdate from your string elements and then convert the nsdate back to a string if you want to :
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm"
let hour = 08
let minutes = 32
let time = formatter.dateFromString("\(hour):\(minutes)")!
let finalTime = formatter.stringFromDate(time)
print(time)
print(finalTime)

I think it's because you're not calling NSCalendar. Since you only want the hour and minutes, you could return a dictionary
func getHourMinutes(fromDate: NSDate) -> [String:Int] {
var timeDic = [String:Int]()
let dC = NSDateComponents()
let cC = NSCalendar.currentCalendar()
if let hour = Int(currentCalendar.component(NSCalendarUnit.Hour, fromDate: fromDate)) {
timeDic.append["hour":hour]
}
if let minutes = Int(currentCalendar.component(NSCalendarUnit.Minute, fromDate: fromDate)) {
timeDic.append["minutes":minutes
}
return timeDic
}
Then you can call the keys to set a new NSDate if you wish.

This is the solution my classmate helped me to achieve
....
let date6 = NSDateComponents()
date6.hour = 17
date6.minute = 3
datesArray = [date2, date3, date4, date5, date6]
datesArray.sortInPlace {
NSNumber(integer:($0.minute + ($0.hour * 60))).compare(NSNumber(integer: ($1.minute + ($1.hour * 60)))) == .OrderedAscending
}
....

Related

How do I programatically determine the time (hours/minutes) and set them to 00/00 in Swift?

I am doing some date math and before doing so am trying to programatically adjust the time associated with a date based on its current value. In the code below I am able to set the hours and minutes to 00/00 but I have to know the offset and manually set the value. Below is my code and next to each print statement I have listed the value I am getting. Any assistance in pointing out the error I am making will be appreciated. I wonder if it is a timezone issue relative to GMT.
Chris
func AdjustDateTime(vooDate: Date) -> Date {
let date = vooDate
let _ = print("date")
let _ = print(date) // returns 2021-10-25 06:00:00 +000
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
let year = components.year
let month = components.month
let day = components.day
let hour = components.hour
let minute = components.minute
let _ = print("hour")
let _ = print(hour!) // returns 0 even though the date above say 06
let _ = print("minute")
let _ = print(minute!) // returns 0
var comps = DateComponents()
comps.year = year
comps.month = month
comps.day = day! + 1
comps.hour = -06 // setting manually, would like to do so programatically
comps.minute = 00
let returnDate: Date = Calendar.current.date(from: comps)!
let _ = print("Return Date")
let _ = print(returnDate) // returns 2021-10-26 00:00:00 +0000, which is what I want
return returnDate
}
Setting the time zone as indicated by Jacob was the key to solving the problem. In the code where I read in the JSON file I modified my dateFormatter to what's shown below. This returns a date object as shown below the code. Now I do not need to worry about the hours, since they are 0. From there it is easy to add 1 day with a function, shown below. Before doing my date math I make the same adjustments to a date in the future, i.e. timezone and locale, and I get the correct difference between the two dates which was the ultimate goal. Thank you for the assistance.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // added
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) // added
2021-10-25 00:00:00 +0000
func AdjustDateTime(vooDate: Date) -> Date {
return Calendar.current.date(byAdding: .day, value: 1, to: vooDate)!
}

Having trouble trying to subtract a number from a date/time and setting that value in a textField

I am working on an app that has a startTime, endTime and duration. The user can set the endTime by click a button and it sets the value to "now" in the format 12:02:03 PM. I then want to be able to enter a duration time in minutes, let's say 20 minutes.
I have everything working where I can read the duration in real time as well as see the current time. The issue is when I try to create a function to subtract the duration from the endTime. I cannot seem to get the syntax or the formatting correct.
I've done quite a bit of searching for examples of this. Here is what I came across so far.
How to add minutes to current time in swift
How to subtract date components?
How to get the current time as datetime
func controlTextDidChange(_ obj: Notification) {
let enteredValue = obj.object as! NSTextField
timeString(time: enteredValue.doubleValue)
}
func timeString(time: TimeInterval) {
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: Date())
let yourDate = formatter.date(from: myString)
formatter.dateFormat = "hh:mm:ss a"
yourDate!.addingTimeInterval(0-time)
let wtf = formatter.string(from: yourDate!)
startTime.stringValue = wtf
}
The controlTextDidChange function is watching the durationTextField and I am able to print to console the input. I then want to be able to run the timeString function with the durationTextField value and subtract it from the endTime and then set that value to startTime.
One thing that is weird is Xcode tells me:
Result of call to 'addingTimeInterval' is unused
You are taking too many steps. Just create a Date that is time seconds from "now". Then convert that Date to a String.
func timeString(time: TimeInterval) {
let startDate = Date(timeIntervalSinceNow: -time)
formatter.dateFormat = "hh:mm:ss a"
let wtf = formatter.string(from: startDate)
startTime.stringValue = wtf
}
I'm assuming you want startDate to be time seconds before now.
It's not weird, the warning tells you that addingTimeInterval creates and returns a new date.
Just do use the Result of call to 'addingTimeInterval'
The conversion Date to String and back to Date is pointless.
func timeString(time: TimeInterval) {
formatter.dateFormat = "hh:mm:ss a"
let newDate = Date().addingTimeInterval(-time)
let wtf = formatter.string(from:newDate)
startTime.stringValue = wtf
}
With the help of vadian and rmaddy I was able to make it work.
Here is my working code
func timeString(time: TimeInterval) {
formatter.dateFormat = "hh:mm a"
let endTimeValue = formatter.date(from: endTime.stringValue)
let newTime = endTimeValue!.addingTimeInterval(-time * 60)
let newtimeString = formatter.string(from:newTime)
startTime.stringValue = newtimeString
}

trigger local notification on specific week day

I'm trying to trigger local notification on specific day of weekday, but all my attempts have failed, the notification should be triggered on specific time of weekday, but instead it's triggered immediately, I searched all possible solutions in stack overflow but non of those answers have solved my problem, how I can solve this problem?
func combineDate(time:NSDate,dayIndex:Int) -> NSDate {
let calendar: NSCalendar = NSCalendar.currentCalendar()
let components: NSDateComponents = calendar.components([.Year, .Month, .WeekOfYear, .Weekday], fromDate: time)
let component1 = NSDateComponents()
component1.weekday = dayIndex
component1.minute = components.minute
component1.hour = components.hour
let combinedDate: NSDate = calendar.dateFromComponents(component1)!
return combinedDate
}
You just have to set the fireDate attribute in the local notification.
One way to create your custom date could be with NSDateFormatter
let stringDate = "2016-05-23 17:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let myDate = formatter.dateFromString(stringDate)
func saveLocalNotification(myDate : NSDate){
let localNotification = UILocalNotification()
localNotification.fireDate = myDate
...
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
To know how to get the weekdays you could take a look at this answer Get day of week using NSDate swift

Swift time being returned as am when it is pm

This function gets current time and finds the next time in an array. When the current time is before midday and the next time is after midday, it returns the next time as am when it should be pm.
How can I change this? Would I need to use a 12 hour clock instead of a 24 hour clock?
import UIKit
import Foundation
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Hour, .Minute], fromDate: date)
let hour = components.hour
let minutes = components.minute
let currentTime = "\(hour)" + ":" + "\(minutes)" //output 10:47
let timesArray = ["5:45", "6:35", "7:00", "7:30", "7:50", "8:20", "8:40", "9:15", "10:10",
"12:40", "14:15", "14:50", "15:40", "16:10", "17:10", "17:40", "18:40", "19:25", "20:50"]
// create a method to convert your time to minutes
func stringToMinutes(input:String) -> Int {
let components = input.componentsSeparatedByString(":")
let hour = Int((components.first ?? "0")) ?? 0
let minute = Int((components.last ?? "0")) ?? 0
return hour*60 + minute
}
//create an array with the minutes from the original array
let timesMinutesArray:[Int] = timesArray.map { stringToMinutes($0) }
let dayMinute = stringToMinutes(currentTime)
// filter out the times that has already passed
let filteredTimesArray = timesMinutesArray.filter{$0 > dayMinute }
// get the first time in your array
if let firstTime = filteredTimesArray.first {
// find its position and extract it from the original array
let nextDeparture = timesArray[timesMinutesArray.indexOf(firstTime)!] // output "12:40"
let userCalendar = NSCalendar.currentCalendar()
let dateMakerFormatter = NSDateFormatter()
dateMakerFormatter.calendar = userCalendar
dateMakerFormatter.dateFormat = "yyyy/MM/dd"
// How many hours and minutes between current time and next departure?
dateMakerFormatter.dateFormat = "h:mm"
let startTime = dateMakerFormatter.dateFromString(currentTime)!
let endTime = dateMakerFormatter.dateFromString(nextDeparture)! //this comes back as 12:40 am not pm
let hourMinuteComponents: NSCalendarUnit = [.Hour, .Minute]
let timeDifference = userCalendar.components(
hourMinuteComponents,
fromDate: startTime,
toDate: endTime,
options: [])
let difference = (timeDifference.hour*60) + (timeDifference.minute)
}
Try a capital H in your dateFormat:
dateMakerFormatter.dateFormat = "H:mm"

To use the conditional branch (If statements) to obtain the time

We want to assign the result Once at 23:00 to number 1, but does not move the following source.
Perhaps, I think not been able conversion of type String.
I want you to tell me the solution
import UIKit
let now = NSDate() // gettime
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "ja_JP")
dateFormatter.dateFormat = "hh" // o`clock
print(dateFormatter.stringFromDate(now)) // convert to String
if now = 23 {
number1 = result
result = 0
}
let now = NSDate() // gettime
let calendar = NSCalendar.autoupdatingCurrentCalendar()
// If you want to know the hour in a particular time zone, set it here.
// For example, to get the time in Japan:
// calendar.timeZone = NSTimeZone(name: "Asia/Tokyo")!
// By default, the calendar uses the system time zone setting.
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: now)
if hour == 23 {
print("It is the appointed hour.")
}