Format hour and minute integers as the String "9:30 am" - swift

How can I express hour and minute integer values as a String formatted like "9:30 am"?
Currently, I have:
let hour: Int = 9
let minute: Int = 30
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let time: String = DateComponentsFormatter.localizedString(from: dateComponents, unitsStyle: DateComponentsFormatter.UnitsStyle.positional)
print(time) // Prints "9:30", not "9:30 am"
I know I can manually concatenate the time meridian at the end, but I'm hoping there's a built in function for this. Perhaps a different UnitsStyle?

You could use DateFormatter to achieve this.
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"
let dateString = formatter.string(from: Date())
print(dateString) // prints "12:17 pm"
If you want to only include single digits for the hour, then you only include one "h" in the dateFormat:
formatter.dateFormat = "h:mm a" // prints "1:30 pm" instead of "01:30 pm"

let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
formatter.calendar = .current
let hour: Int = 9
let minute: Int = 30
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
If let fixedDate: Date = Calendar.current.date(from: dateComponents) {
let formattedString = formatter.string(from: fixedDate)
print(formattedString) //prints 09:30 AM
}
You need to add current calendar/date to get am or pm from your time from what I know.
EDIT:
Thanks to Leo Dabus in the comments for pointing this out: the above method will result in a date that is on January 1st 0001, if the date is important for you you have to specify the date (day/month/year)
for example:
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
formatter.calendar = .current
let hour: Int = 9
let minute: Int = 30
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.year = Calendar.current.component(.year, from: date)
dateComponents.month = Calendar.current.component(.month, from: date)
dateComponents.day = Calendar.current.component(.day, from: date)
If let fixedDate: Date = Calendar.current.date(from: dateComponents) {
let formattedString = formatter.string(from: fixedDate)
print(formattedString)
}

Related

how to use DateComponentsFormatter for showing date as "Today" or "Yesterday"?

I'm using swift in my project and I want to show specific date like this: 5 days ago or 5 month ago or ...
I am using DateComponentsFormatter and it's doing well, but the problem is I want to show 1 day ago like "Yesterday" or showing 3 second ago like "Today". how can I do this? can I use DateComponentsFormatter for this problem? this is my codes:
func shortDate(_ local: LocaleIdentifier = .fa) -> String {
let now = Date()
let date = self
let formatter = DateComponentsFormatter()
formatter.calendar?.locale = Locale(identifier: local.rawValue)
formatter.unitsStyle = .full
formatter.maximumUnitCount = 1
formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]
guard let timeString = formatter.string(from: date, to: now) else {
return ""
}
return String(format: "Ago".localized, timeString)
}
For iOS 13 or later you can use RelativeDateTimeFormatter
let relativeDateTimeFormatter = RelativeDateTimeFormatter()
relativeDateTimeFormatter.dateTimeStyle = .named
relativeDateTimeFormatter.unitsStyle = .full
let date = Date.init(timeIntervalSinceNow: -60*60*24)
relativeDateTimeFormatter.string(for: date) // "yesterday"
edit/update:
If you would like to support iOS 11 you would need to implement your own relative date formatter. You can use Calendar method isDateInToday and isDateInYesterday to combine a relative date formatter with date components formatter. Note that there is no need to check the time interval for setting a single unit in your date components formatter you can set the allowed units of your date components formatter, just make sure you set them respecting the desired priority to be displayed:
// This will avoid creating a formatter every time you call relativeDateFormatted property
extension Formatter {
static let dateComponents: DateComponentsFormatter = {
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.day, .month, .year] // check the order of the units it does matter when allowing only 1 unit to be displayed
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.unitsStyle = .full
return dateComponentsFormatter
}()
static let relativeDate: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.doesRelativeDateFormatting = true
dateFormatter.timeStyle = .none
dateFormatter.dateStyle = .medium
return dateFormatter
}()
}
extension Date {
var relativeDateFormatted: String {
Calendar.current.isDateInToday(self) || Calendar.current.isDateInYesterday(self) ?
Formatter.relativeDate.string(from: self) :
Formatter.dateComponents.string(from: self, to: Date()) ?? ""
}
}
Playground testing:
let date1 = DateComponents(calendar: .current, year: 2020, month: 9, day: 4, hour: 5).date!
let date2 = DateComponents(calendar: .current, year: 2020, month: 9, day: 3, hour: 23, minute: 50).date!
let date3 = DateComponents(calendar: .current, year: 2020, month: 8, day: 25, hour: 10).date!
let date4 = DateComponents(calendar: .current, year: 2020, month: 8, day: 3).date!
let date5 = DateComponents(calendar: .current, year: 2019, month: 8, day: 27).date!
date1.relativeDateFormatted // "Today"
date2.relativeDateFormatted // "Yesterday"
date3.relativeDateFormatted // "10 days"
date4.relativeDateFormatted // "1 month"
date5.relativeDateFormatted // "1 year"
You are looking for the doesRelativeDateFormatting property of a DateFormatter.
I think, I must use both DateComponentsFormatter for more than 1 day and DateFormatter for showing Today(day=0) and Yesterday(day=1) for my problem.
I found my answer here.
but I make codes a little better, this is my codes:
var calendar = Calendar.current
calendar.locale = Locale(identifier: local.rawValue)
let componentFormatter = DateComponentsFormatter()
componentFormatter.calendar = calendar
componentFormatter.unitsStyle = .full
componentFormatter.maximumUnitCount = 1
let interval = Calendar.current.dateComponents([.year, .month, .day], from: self, to: Date())
if let year = interval.year, year > 0 {
componentFormatter.allowedUnits = .year
} else if let month = interval.month, month > 0 {
componentFormatter.allowedUnits = .month
} else if let day = interval.day, day > 1 {
componentFormatter.allowedUnits = .day
} else { // if let day = interval.day, day == 0 || day == 1 for Today or Yesterday
let formatter = DateFormatter()
formatter.doesRelativeDateFormatting = true
formatter.calendar.locale = Locale(identifier: local.rawValue)
formatter.timeStyle = .none
formatter.dateStyle = .medium
return formatter.string(from: self)
}
guard let timeString = componentFormatter.string(from: self, to: Date()) else {
return ""
}
return String(format: "Ago".localized, timeString)

How do you get all the 4 (or 5) separate weeks in a month from Date() in swift?

I managed to get data query with "nextDate" property of type Date() in a particular month. In other words all data with dates within that particular month will appear on my tableview when queried. Using the code below and with tweaks I also managed to extract data from the previous and next month.
func loadMonthEvents() {
let date = Date()
let calendar = Calendar.current
var beginningOfMonth: Date?
var endOfMonth: Date?
beginningOfMonth = calendar.dateInterval(of: .month, for: date)?.start
endOfMonth = calendar.dateInterval(of: .month, for: date)?.end
monthEvents = realm.objects(Events.self).filter("nextDate BETWEEN %#", [beginningOfMonth, endOfMonth]).sorted(byKeyPath: "nextDate", ascending: true)
}
Now I want to be able to separate those data according to the week of the month. In my tableView the will be 5 separate headers representing week 1, week 2, week 3, week 4 and week 5(if any). Each separate header will show only event for that week. I tried to apply the weekOfMonth in calendar but it just dont work. Thank you in advance.
You could get the range of weeks from your two dates by doing
let w1 = calendar.dateComponents([.weekOfYear], from: beginningOfMonth!)
print(w1.weekOfYear)
let w2 = calendar.dateComponents([.weekOfYear], from: endOfMonth!)
print(w2.weekOfYear)
And then you could do the same for the date of each event to group the event to a specifik week
Here is the snippet to find out first and last day of week . You can do this by adding .weekOfMonth to the date component . Go through this official link and apply accordingly as per your requirement for fetch.
Now i had added two functions / or two button action through which you can get previous week and next week of the month.
var currentDate = Date()
func weekCalculation()
{
let calendar = NSCalendar.current
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// dateFormatter.dateStyle = .medium
var componentsNow = calendar.dateComponents([.year, .month, .weekOfMonth, .weekday], from: currentDate)
componentsNow.setValue(1, for: .weekday)
firstDayOfWeek = calendar.date(from: componentsNow)!
print(firstDayOfWeek)
componentsNow.setValue(7, for: .weekday)
lastDayOfWeek = calendar.date(from: componentsNow)!
print(lastDayOfWeek)
let addDaysCount = 0
var comps = DateComponents()
comps.setValue(addDaysCount, for: .weekday)
var comps1 = DateComponents()
comps1.setValue(-6, for: .day)
let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
// print(newDate1!,newDate2!)
let firstDay = dateFormatter.string(from: newDate1!)
let lastDay = dateFormatter.string(from: newDate2!)
// ShowBanner(title: "", subtitle: firstDay)
let dF = DateFormatter()
dF.dateFormat = "d MMMM yyyy"
let fDayToShow = dF.string(from: newDate1!)
let lDayToShow = dF.string(from: newDate2!)
let dateString = String(format: "%# - %#",fDayToShow,lDayToShow)
print(firstDay,lastDay)
}
#IBAction func nextWeekBtnPressed(sender: UIButton)
{
let calendar = NSCalendar.current
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// dateFormatter.dateStyle = .medium
let dF = DateFormatter()
dF.dateFormat = "d MMMM yyyy"
let addDaysCount = 7
var comps = DateComponents()
comps.setValue(addDaysCount, for: .weekday)
var comps1 = DateComponents()
comps1.setValue(3, for: .day)
let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
let firstDay = dateFormatter.string(from: newDate1!)
let lastDay = dateFormatter.string(from: newDate2!)
let fDayToShow = dF.string(from: newDate1!)
let lDayToShow = dF.string(from: newDate2!)
//print(firstDay,lastDay)
let dateString = String(format: "%# - %#",fDayToShow,lDayToShow)
//print(dateString)
weekCalculation()
}
#IBAction func previousWeekBtnPressed(sender: UIButton)
{
let calendar = NSCalendar.current
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// dateFormatter.dateStyle = .medium
let addDaysCount = -7
var comps = DateComponents()
comps.setValue(addDaysCount, for: .weekday)
var comps1 = DateComponents()
comps1.setValue(-10, for: .day)
let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
let firstDay = dateFormatter.string(from: newDate1!)
let lastDay = dateFormatter.string(from: newDate2!)
let dF = DateFormatter()
dF.dateFormat = "d MMMM yyyy"
let fDayToShow = dF.string(from: newDate1!)
let lDayToShow = dF.string(from: newDate2!)
let dateString = String(format: "%# - %#",fDayToShow,lDayToShow)
weekCalculation()
}

Convert time string to Unix time date of the same day

how to convert a string hour to millisecond of the day?
for exemple:
let strDate = "06:00 PM"
Or:
let strDate = "09:00 AM"
my code:
let dateString = "06:00 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
guard let date = dateFormatter.date(from: dateString)
else { fatalError() }
print(date)
for example my string is: 06:00 PM, so I want to have the date in millisecond of today Thursday 20 September 2018 at 06:00 PM
You can set your DateFormatter default date to startOfDay for today, set the formatter locale locale to "en_US_POSIX" when parsing your time then you can simply get your resulting date timeIntervalSince1970 and multiply by 1000:
let strTime = "06:00 PM"
let formatter = DateFormatter()
formatter.defaultDate = Calendar.current.startOfDay(for: Date())
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "hh:mm a"
if let date = formatter.date(from: strTime) {
let milliseconds = date.timeIntervalSince1970 * 1000
print(milliseconds) // 1537477200000
let date2 = Date(timeIntervalSince1970: milliseconds/1000)
print(date2.description(with: .current)) // Thursday, September 20, 2018 at 6:00:00 PM Brasilia Standard Time
}
First, you need to parse the date string:
let dateString = "06:00 PM"
let formatter = DateFormatter()
formatter.defaultDate = Date()
formatter.dateFormat = "hh:mm a"
formatter.locale = Locale(identifier: "en_US_POSIX")
let date = formatter.date(from: dateString)
Then, you need to get the start of day:
let calendar = Calendar.current
let start = calendar.startOfDay(for: date)
After that, get the time interval between date and start:
let timeInterval = date.timeIntervalSince(start)
let milliseconds = timeInterval * 1000.0

String conversion to sec min and hour

i have a time string
duration = "00:01:28"
i want to show like 28 sec ,1 min. how to get separate component as seconds,minute and hour.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let date = dateFormatter.date(from:duration)
let calendar = Calendar.current
let comp = calendar.dateComponents([.hour, .minute], from: date!)
let hour = comp.hour
let minute = comp.minute
let sec = comp.second
print(sec)
print(minute)
I tried this code but I got a nil in the print statement.
Almost. Of course sec prints nil because you didn't specify the second component.
To print the requested string format I recommend DateComponentsFormatter
let duration = "00:01:28"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let date = dateFormatter.date(from:duration)
let components = Calendar.current.dateComponents([.hour, .minute, .second], from: date!)
let componentsFormatter = DateComponentsFormatter()
componentsFormatter.allowedUnits = [.hour, .minute, .second]
componentsFormatter.unitsStyle = .short
componentsFormatter.string(from: components) // "1 min, 28 secs"

NSDateFormatter. Setting time elapsed since date in swift

I'm looking to call a function that checks time elapsed since date. This will determine how the timeLable displays in my messages view controller, similar to IMessage.
The code I'm using below only shows HH:MM
let date = dateFormatter().dateFromString((recent["date"] as? String)!)
timeLabel.text = NSDateFormatter.localizedStringFromDate(date!, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.NoStyle)
I'm looking to change it to something along the lines of:
If date is today, date = "HH:MM"
If date is Yesterday, date = "Yesterday"
If date is the day before yesterday and so on, date = "Monday, Tuesday, Wednesday..."
If date is over 1 week, date = MM/DD/YY
Or try this. Note that we have to use components:fromDate: and then use components:fromDateComponents:toDateComponents:options: because if we don't 23:59 last night returns 23:59 instead of Yesterday.
extension NSDateFormatter {
static func friendlyStringForDate(date:NSDate) -> String {
// Fetch the default calendar
let calendar = NSCalendar.currentCalendar()
// Compute components from target date
let from = calendar.components([.Day, .Month, .Year], fromDate: date)
// Compute components from current date
let to = calendar.components([.Day, .Month, .Year], fromDate: NSDate())
// Compute days difference between the two
let delta = calendar.components(.Day, fromDateComponents: from, toDateComponents: to, options: [])
switch delta.day {
case 0:
let formatter = NSDateFormatter()
formatter.timeZone = NSTimeZone.defaultTimeZone()
formatter.dateFormat = "HH:mm"
return formatter.stringFromDate(date)
case 1:
return "Yesterday"
case 2..<7:
let formatter = NSDateFormatter()
formatter.timeStyle = .NoStyle
formatter.dateFormat = "EEEE"
return formatter.stringFromDate(date)
default:
let formatter = NSDateFormatter()
formatter.timeStyle = .NoStyle
formatter.dateFormat = "MM/dd/YY"
return formatter.stringFromDate(date)
}
}
}
Now then, to use it just:
timeLabel.text = NSDateFormatter.friendlyStringForDate(date!)
SWIFT 3:
extension DateFormatter {
static func friendlyStringForDate(date: Date) -> String {
// Fetch the default calendar
let calendar = Calendar.current
let unitFlags: NSCalendar.Unit = [.day]
// Compute days difference between the two
let delta = (calendar as NSCalendar).components(unitFlags, from: date, to: Date(), options: [])
if let day = delta.day {
switch day {
case 0:
let formatter = DateFormatter()
formatter.timeZone = NSTimeZone.default
formatter.dateFormat = "hh:mm a"
return formatter.string(from: date)
case 1:
return "Yesterday"
case 2..<7:
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateFormat = "EEEE"
return formatter.string(from: date)
default:
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateFormat = "MM/dd/YY"
return formatter.string(from: date)
}
}
return ""
}
}