I am trying to get the current date using swift. I have that taken care of but it gets the year month and day and I really only want the current hour and minute. The reason being is that I am trying to later on compare a created date that only has an hour and minute. I am currently just using this
let currentDate = NSDate()
Thank you
let calendar = Calendar.autoupdatingCurrent
let components = calendar.dateComponents([.hour, .minute], from: Date())
let hour = components.hour
let minute = components.minute
Related
Currently my method for determining wether data is older than n hours uses this code after getting a timestamp from a document:
let timestamp = i.get("timestamp") as! Timestamp
let lastUpdatedDate = timestamp.dateValue()
let currentDate = Date()
let cal = Calendar.current
let components = cal.dateComponents([.hour], from: lastUpdatedDate, to: currentDate)
let diff = components.hour!
Usually this code is executed in a for each loop per each document after getting the documents from firestore.
Is there a way to query data using the field timestamp and checking if it is less than n hours instead?
ref.collection("References").whereField("timestamp", isLessThanOrEqualTo: n hours)
If you want a value to compare it against, you can do:
let date = Date().addingTimeInterval(-4 * 60 * 60)
Or you can do:
let date = Calendar.current.date(byAdding: .hour, value: -4, to: Date())
You may have to convert those to a Timestamp from this Date object, e.g., presumably something like:
let timestamp = Timestamp(date)
I'm working on fitness app where users selects the days he wants to exercise on.
When he opens the app I wanna shown him the current week where he can observe the days his training sessions are scheduled for.
If he is from the US i wanna show him a week starting from Sunday. For EU users it should start with Monday.
Is there any way to get the "current" week dates depending on user's location/geo? Taking into account what day does the week start with in appropriate location.
I tried to find a solution for your question. I think this should work:
// Define a function that returns the following seven dates, given a start date
func getWeekDates(of startDate: Date, with calender: Calendar) -> [Date] {
var weekDates: [Date] = []
for i in 0..<7 {
weekDates.append(calendar.date(byAdding: .day, value: i, to: startDate)!)
}
return weekDates
}
// This should automatically take the right calendar for the user's locale
// If you want to specify the day weeks start with manually, choose .gregorian or .iso8601:
// .gregorian starts on Sunday, .iso8601 starts on Monday
let calendar = Calendar.current
let startOfCurrentWeek = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))
let currentWeekDates = getWeekDates(of: startOfCurrentWeek!, with: calendar)
Hope this helps.
Use
Calendar.current.firstWeekday
If it returns 1, then Sunday is the first day of week
If it returns 2, then Monday is the first day of week.
You can test this by setting locale manually
var calendar = Calendar.current
calendar.locale = Locale(identifier: "en_GB")
print("\(calendar.locale!) starts on day \(calendar.firstWeekday)")
// en_GB starts on day 2
calendar.locale = Locale(identifier: "en_US")
print("\(calendar.locale!) starts on day \(calendar.firstWeekday)")
So I have a program which I need to ask a user for a date and time. But that date has to either be in the future or the the very far past. And I have tried a UIDatePicker but have found out that a date picker isn't the best way to tend to this sort of problem because it requires a lot of scrolling, and the it doesn't load past a certain date for some reason. I was thinking of a UIPickerView that asks for Year, Month, Day, and Time. But I don't know how to do it because lets say that the month is September there is only 30 days so then how can I stop the user from selecting 31. So if anyone can help me that would be amazing. And if you have any questions or need for code just ask. Oh and by the way I would like it in a date format where I can compare it to other date.
Given the year and month, you can calculate the array of possible day values to be supplied to your picker like so:
let calendar = Calendar.current
let components = DateComponents(year: year, month: month, day: 1)
guard let date = calendar.date(from: components),
let range = calendar.range(of: .day, in: .month, for: date) else { return }
let days = Array(range)
I would like it in a date format where I can compare it to other date.
Once you have the year, month, and day, hour (converted to 24 hour value, from 0 to 23), you can build the Date object like so:
let calendar = Calendar.current
let components = DateComponents(year: year, month: month, day: day, hour: hour, minute: minute)
guard let date = calendar.date(from: components) else { return }
That date can now be compared to any other Date object.
I have two date pickers. Let's say fromDatePicker and untilDatePicker. I set the untilDatePicker to current date (ex: 12/06/17) and I want to set the fromDatePicker into first date of the current month (ex: 01/06/17) but I don't have any idea to set the untilDatePicker.
I have read this question but it's not helped me much.
This is swift3 solution and how you set first day of current month in datePicker , if you need to get first of other month just change date variable with some day from that month
let date = NSDate()
let calendar = NSCalendar.current
let components = calendar.dateComponents([.year,.month], from: date as Date)
let startOfMonth = calendar.date(from: components)
self.datePicker.setDate(startOfMonth!, animated: true)
Would anyone be able to tell me how to separate the date and time components of NSDate() in Swift? I saw a few things about separating the date itself into components (day, month, year), but nothing really about how to get the time only or the day, month, and year only.
Basically, I'm trying to filter data on a table by date, but to also display the time the entry was made. I tried using the plain ol' NSDate(), but then the filtering didn't work because filtering by just today's date didn't yield results because the times those entries were made will always be less than the current time.
You need to use Calendar to break a date apart, using CalendarUnit to specify what components you want.
let calendar = Calendar.current
let components = calendar.dateComponents([.month, .day, .year, .hour, .minute], from: Date())
print("Hour: \(components.hour)")
print("Minute: \(components.minute)")
print("Day: \(components.day)")
print("Month: \(components.month)")
print("Year: \(components.year)")
Swift 5 Xcode 14.2
let myDate = .... // you date here
let cal = Calendar.current
let required : Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let components : DateComponents = cal.dateComponents(required, from: myDate)
print("Hour: \(components.hour)")
print("Minute: \(components.minute)")
print("Day: \(components.day)")
print("Month: \(components.month)")
print("Year: \(components.year)")