How to get Int from DateFormatter() in Swift3 - swift

I want to remove some data from my UITableView when the data is passed over 24 hours. I saved date as String. I want to check the date but because it is String, how am I able to turn that data into Int value in order to check how many hours have passed since the data is last entered? Is there any good code for that to happen? This date data is stored in Firebase.
let currentDateTime = Date()
let formatter = DateFormatter()
formatter.timeStyle = .medium
formatter.dateStyle = .long
let date = formatter.string(from: currentDateTime)

You need:
Your date
Add 24 hours to that date
Compare that new date with "now"
I am assuming you have some date already:
let myDate: Date = ...
Let's add 24 hours:
let endDate: Date = Calendar.current.date(byAdding: .hour, value: 24, to: myDate, wrappingComponents: true)!
Did 24 hours already passed since my date?
let now = Date()
let expired = (endDate < now)
I am not sure why would you even use a date formatter for this task.

Try this:
let currentDateTime = Date()
let formatter = DateFormatter()
formatter.dateFormat = "hh"
let date = formatter.string(from: currentDateTime)
print(date)// Time in string
print(Int(date)!) // Time in Int

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)!
}

Date from String in Swift but it becomes the previous day

I am trying to create a day from a string (mSince) :
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let newDate = dateFormatter.date(from:mSince as! String)!
and the print values are:
mSince = Optional(2021-04-25)
newDate = 2021-04-24 21:00:00 +0000
I could not figure out why newDate becomes the previous day.
Thank you!
Below is an example of how to create dates from multiple UTC timezones and an example of how to get the delta (in seconds) between the dates:
extension Date {
static func - (lhs: Date, rhs: Date) -> TimeInterval {
return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}
}
let utcDateFormatter = DateFormatter()
utcDateFormatter.dateStyle = .medium
utcDateFormatter.timeStyle = .medium
// Set the timeZone to the device’s local time zone.
utcDateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date1 = Date()
print(utcDateFormatter.string(from: date1))
// Parsing a string date
let dateString1 = "May 11, 2020 at 4:23:11 AM"
let utcDate1 = utcDateFormatter.date(from: dateString1)
// Set a date for a different timeZone (2 hours difference)
utcDateFormatter.timeZone = TimeZone(secondsFromGMT: 3600)
// Printing a Date
let date2 = Date()
print(utcDateFormatter.string(from: date2))
// Parsing a string representing a date
let dateString2 = "May 11, 2020 at 4:23:11 AM"
let utcDate2 = utcDateFormatter.date(from: dateString2)
let diff = utcDate1! - utcDate2!
print(diff)

Compare two date - Swift

How can I convert string like (2019-11-02) without time to date format and get current Date device without time then compare with together?
As well as the above using string representations of date, you can actually work with just the dates themselves. Just converting a the string will give you a "start of day" date. The Calendar has a method which will do the same with a date, allowing you to compare the converted string to 'today'
func isDateToday(dateString: String) -> Bool {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let date = df.date(from: dateString)
let today = Calendar.current.startOfDay(for: Date())
return date == today
}
You can convert the string to date using a dateFormatter then compare with the current date using an if statement
import Foundation
//convert string to date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let myDate = dateFormatter.date(from: "2019-11-02")
//convert today's date in the same formate
let currentFormatter = DateFormatter()
currentFormatter.dateStyle = .short
currentFormatter.dateFormat = "yyyy-MM-dd"
let today = currentFormatter.string(from: Date())
let todayDate = dateFormatter.date(from: today)
//Compare the two date's
if myDate == todayDate {
print("ok")
}

How to change time format from string in Swift 3.0

i have a string like "22:02:20" and need to format it to "10:02:00" i have try this:
let time = "22:02:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let calendar = Calendar.current
let unitFlags = Set<Calendar.Component>([.hour, .minute])
staticHoraInicio = calendar.dateComponents(unitFlags, from: dateFormatter.date(from: time))
but i get 22 and 2, how can i get 10 and 02. Also i try:
let horaF = dateFormatter.date(from: time!)
but i get something like "2000-01-02 03:00:00 +0000", ¿there is a way to format just a time string without date?
I need this to store a start time and end time coming from a DB, compare it to the current time to get if it's between, some example about how to do this???
let time = "22:02:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
var fullDate = dateFormatter.date(from: time)
dateFormatter.dateFormat = "hh:mm:ss a"
var time2 = dateFormatter.string(from: fullDate!)

Show Date with DateFormatter

I'm looking for a way to display the current date in a label with Swift 3.
Currently I'm using DateFormatter.dateFormat but the only output I get is
dd.MM.yyyy
Here is the line of code:
lbl_Date.text = DateFormatter.dateFormat(fromTemplate: "MMddyyyy", options: 0, locale: NSLocale(localeIdentifier: "de-AT") as Locale)
And my expected output for today should be:
11.12.2016
I know that I can get the time with the dateTimeComponents but I haven't figured out how I can get only the date (day, month and year) in my label:
// get the current date and time
let currentDateTime = Date()
// get the user's calendar
let userCalendar = Calendar.current
// choose which date and time components are needed
let requestedComponents: Set<Calendar.Component> = [
.year,
.month,
.day,
.hour,
.minute,
.second
]
// get the components
let dateTimeComponents = userCalendar.dateComponents(requestedComponents, from: currentDateTime)
What is there the cleanest way to display the current date in my label?
Forget Calendar and DateComponents. Use DateFormatter
let currentDateTime = Date()
let formatter = DateFormatter()
If you want a fixed format set the dateFormat to
formatter.dateFormat = "dd.MM.yyyy"
alternatively if you want a format depending on the current locale set timeStyle and dateStyle rather than the format property
formatter.timeStyle = .none
formatter.dateStyle = .medium
Now get the string
let dateString = formatter.string(from: currentDateTime)