How can I get the current month as String?
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)
get every name of month components in swift
The DateFormatter
class has what you need:
let formatter = DateFormatter()
let monthComponents = formatter.shortMonthSymbols
See the docs for DateFormatter
to see all of the other related methods for getting other various symbols.
Or as of iOS 10/macOS 10.12 you can use Calendar
:
let monthComponents = Calendar.current.shortMonthSymbols
Populate objects in TableView sections by months order
Create a wrapper struct
struct Section {
let month : String
let birthdays : [Birthday]
}
then map the grouped dictionary to an array of Section
let keys = Array(grouped.keys)
let sections = keys.map({Section(month: $0, birthdays: grouped[$0]!)})
sections
represent the sections, birthdays
the rows, month
the section header titles.
Edit:
To be able to sort the months by their ordinal number return the number in front of the month name
grouped = Dictionary(grouping: birthdaysList) { item -> String in
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month], from: item.date)
let date = calendar.date(from: components) ?? Date(timeIntervalSince1970: 0)
return "\(components.month!)_" + date.monthAsString()
}
And modify the mapping code
let keys = grouped.keys.sorted{$0.localizedStandardCompare($1) == .orderedAscending}
let sections = keys.map({Section(month: $0.components(separatedBy:"_").last!, birthdays: grouped[$0]!)})
Create date by components in swift
Try adding "GMT" timezone, such as:
dateComponents.timeZone = TimeZone(identifier: "GMT")
How to get DatePicker time to Calendar component and insert to Label component in Swift
I think you want something like this:
@IBAction func datePickerChanged(_ sender: UIDatePicker) {
var pickerDate = sender.date
let calendar = Calendar.current
guard let endDate = calendar.date(byAdding: .hour, value: 8, to: pickerDate) else { return }
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.short //show time in h:mm format
dateFormatter.locale = Locale(identifier: "de-DE") //locale Germany
var endTimeString = dateFormatter.string(from: endDate) //convert time from date to String
dateLabel.text = endTimeString // show calculated time in UILabel
}
First, you need to get the selected date from the UIDatePicker.
Then, for date calulations, you need to specify a calendar to work with. Typically the current calendar is what you want (this is the calendar specified for the device). Then, add 8 hours and convert it to a string.
Btw.: you need to keep in mind that when the user picks a date in the evening, there is a "day flip" when you add 8 hours.
Update
To add somthing like 8 hours and 13 minutes, it's best to use DateComponents
:
var now = Date()
var calendar = Calendar.current
var addComp = DateComponents(calendar:calendar,
hour:8,
minute:13)
if let then = calendar.date(byAdding: addComp, to: now, wrappingComponents:false) {
print(now)
print(then!)
}
Swift - New Update gives optional issues
You need to unwrap the variables with !
when you´re creating your timeString
and dateString
:
let timeString = "\(hour!):\(minutes!):\(seconds!)"
var dateString = "\(year!)/\(month!)/\(day!)"
print(timeString)
print(dateString)
Will print out:
6:0:25
2016/9/16
Related Topics
Objective-C Check If Subviews of Rotated Uiviews Intersect
How to Efficiently Find Cgrects for Visible Words in Uitextview
How to Use Coreaudio's Audioconverter to Encode Aac in Real-Time
How to Upload Video to Server from Iphone
Can Swift Closures Be Set to a Default Value When Used as a Parameter in a Function
Xcode/Swift 'Filename Used Twice' Build Error
Flutter:Not Connecting to iOS Simulator in Android Studio
Move Xcode Project Causes Red Text (Missing Files from Project)
Uiview/Calayer: Transform Triggers Layoutsubviews in Superview
Using Multiple Storyboards with a Tabbarcontroller
iOS Crash When Testing on Device - Debug Logs
About Viewcontroller's "Viewdidload" and "Viewwillappear" Methods
How to Merge Audio and Video Using Avmutablecompositiontrack