I need to get the age after the datepicker has been used. How would I do this. I can currently get the datepicker to work.
#IBAction func datepickerobj(sender: UITextField) {
let datePickerView:UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
dateofbirth.text = dateFormatter.stringFromDate(sender.date)
let myDOB = NSCalendar.currentCalendar().dateWithEra(1, year: 1970, month: 09, day: 10, hour: 0, minute: 0, second: 0, nanosecond: 0)!
let myAge = myDOB.age
}
Swift 4.1 / Xcode 9.4.1
func age(dateOfBirth: Date) -> Double {
var ageComponents: DateComponents = Calendar.current.dateComponents([.year], from: dateOfBirth, to: Date())
return Double(ageComponents.year!)
}
This will work fine for u
var birthday: NSDate = ..... //date that comes from date picker
var now: NSDate = NSDate()
var ageComponents: NSDateComponents = NSCalendar.currentCalendar().components(.Year, fromDate: birthday, toDate: now, options: 0)
var age: Int = ageComponents.year()
If u want to formate date you can use DateFormatter
Ex.
let usDateFormat = NSDateFormatter.dateFormatFromTemplate("MMddyyyy", options: 0, locale: NSLocale(localeIdentifier: "en-US"))
//usDateFormat now contains an optional string "MM/dd/yyyy".
let gbDateFormat = NSDateFormatter.dateFormatFromTemplate("MMddyyyy", options: 0, locale: NSLocale(localeIdentifier: "en-GB"))
//gbDateFormat now contains an optional string "dd/MM/yyyy"
formatter.dateFormat = usDateFormat
let usSwiftDayString = formatter.stringFromDate(swiftDay)
// usSwiftDayString now contains the string "06/02/2014".
formatter.dateFormat = gbDateFormat
let gbSwiftDayString = formatter.stringFromDate(swiftDay)
// gbSwiftDayString now contains the string "02/06/2014".
//may be it will help for u
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy"
let currentYear: Int = Int(formatter.stringFromDate(NSDate()))
let dobYear: Int = Int(formatter.stringFromDate(myDOB))
let age: Int = currentYear - dobYear
extension Date {
func age() -> Int {
return Int(Calendar.current.dateComponents([.year], from: self, to: Date()).year!)
}
}
An Date extension version, which let you call the method on any Date var. eg:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day], fromDate: NSDate.init())
components.day = 31
components.month = 1
components.year = 1979
let date = calendar.dateFromComponents(components)
let years = date.age
Related
I have two dates, date1 and date2 and I want days between date1 and date 2
Example:
let date1 = 28-May-2019,
let date2 = 31-May-2019
The expected output
[Tue, Web Thr, Fri]
let date1Str = "28-May-2019"
let date2Str = "31-May-2019"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.locale = Locale(identifier: "en_US")
var date1 = dateFormatter.date(from:date1Str)!
var date2 = dateFormatter.date(from:date2Str)!
let dayFormatter = DateFormatter()
dayFormatter.dateFormat = "EEE"
while date1 <= date2 {
let dayInWeek = dayFormatter.string(from: date1)
print(dayInWeek)
date1 = Calendar.current.date(byAdding: .day, value: 1, to: date1)!
}
The following code gives you the days between two dates and should account for trickeries with the calendar.
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let dayFormatter = DateFormatter()
dayFormatter.dateFormat = "EEE"
let dateFrom = dateFormatter.date(from: "28-May-2019")!
let dateTo = dateFormatter.date(from: "31-May-2019")!
var days: [String] = []
var date = dateFrom
while date <= dateTo {
let day = dayFormatter.string(from: date)
days.append(day)
date = calendar.date(byAdding: .day, value: 1, to: date)!
}
print(days)
Try this -
func getWeekdays(dateOne firstDateStr: String, dateTwo secondDateStr: String) -> [String] {
let dateformatter = DateFormatter()
dateformatter.dateFormat = "dd-MMM-yyyy"
guard let firstDate = dateformatter.date(from: firstDateStr),
let secondDate = dateformatter.date(from: secondDateStr) else {
return []
}
let calendar = Calendar.current
let numberOfDays: Int
if firstDate > secondDate {
numberOfDays = (calendar.dateComponents([.day], from: secondDate, to: firstDate).day ?? 0)
} else {
numberOfDays = (calendar.dateComponents([.day], from: firstDate, to: secondDate).day ?? 0)
}
dateformatter.dateFormat = "EEE"
let days = (0...numberOfDays).compactMap { day -> String? in
if let date = calendar.date(byAdding: .day, value: day, to: firstDate) {
return dateformatter.string(from: date)
}
return nil
}
print(days)
return days
}
I have a UIDatePicker which displays the date using the Islamic Calendar. The datePicker is displaying the values in the correct format. However, when I assign the users selection to the UITextField, it unfortunately displays the equivalent date in Gregorian calendar format. How can I change this so that it displays the Islamic calendar equivalent?
Here is the relevant code:
//The below code is what displays the dates from the Islamic calendar in the UIDatePicker (and works correctly):
let islamicCalendar = NSCalendar.init(calendarIdentifier: NSCalendarIdentifierIslamic)
let components: NSDateComponents = NSDateComponents()
components.calendar = islamicCalendar
components.year = 10
let currentDate = NSDate()
let maxDate: NSDate = islamicCalendar!.dateByAddingComponents(components, toDate: currentDate, options: [])!
self.datePicker?.calendar = islamicCalendar
self.datePicker?.minimumDate = NSDate()
self.datePicker?.maximumDate = maxDate
This is the block of code which converts the users selection and assigns it to the appropriate UITextField:
var selectedDate: String = String()
selectedDate = self.dateformatterDateTime(self.datePicker!.date) as String
myTextField.text = selectedDate
func dateformatterDateTime(date: NSDate) -> NSString {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
return dateFormatter.stringFromDate(date)
}
You should either convert the selected date into islamic date in your func dateformatterDateTime(date: NSDate) -> NSString method or send this method a converted date. You can do it so by -
var selectedDate: String = String()
var date = (islamicCalendar?.date(byAdding:components, to: self.datePicker!.date, options: []))!
selectedDate = self.dateformatterDateTime(date) as String
myTextField.text = selectedDate
func dateformatterDateTime(date: NSDate) -> NSString {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
return dateFormatter.stringFromDate(date)
}
or -
func dateformatterDateTime(date: Date) -> NSString {
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var dateToSendBack = (islamicCalendar?.date(byAdding:components, to: date, options: []))!
return dateFormatter.string(from: dateToSendBack)
}
try this,
let picker = UIDatePicker(frame: CGRect(x: 0, y: 30, width: 0, height: 0))
picker.datePickerMode = .date
picker.date = Date()
picker.calendar = Calendar(identifier: .islamicUmmAlQura)
picker.autoresizingMask = UIViewAutoresizing.flexibleRightMargin
picker.frame.size.width = 300
view.addSubview(picker)
I need to get back the localized format for hours and minutes. The hours works as expected but the minutes returns "m"? Why is it not returning "mm"?
let locale = NSDateFormatter().locale
NSDateFormatter.dateFormatFromTemplate("HH", options: 0, locale:locale)
//Returns "HH" in US English and "H时" in Chinese (Simplified)
NSDateFormatter.dateFormatFromTemplate("mm", options: 0, locale:locale)
//Returns "m" for both???
Have you tried using NSDateComponents?
let date: NSDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute, fromDate: date)
components.hour = 20
components.minute = 10
if let newDate = calendar.dateFromComponents(components) {
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.stringFromDate(newDate)
}
Here again as function to retrieve the value you need:
func formattedTimeString(unitToSet: NSCalendarUnit, selectedValue: Int, template: String) -> String?
{
let date: NSDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute, fromDate: date)
if unitToSet == .CalendarUnitHour
{
components.hour = selectedValue
} else
{
components.minute = selectedValue
}
if let newDate = calendar.dateFromComponents(components) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate(template, options: 0, locale: NSLocale.currentLocale())
return dateFormatter.stringFromDate(newDate)
}
return nil
}
if let hours: String = formattedTimeString(.CalendarUnitHour, 22, "HH")
{
println(hours) // prints 22
}
if let minutes: String = formattedTimeString(.CalendarUnitMinute, 33, "mm")
{
println(minutes) // prints 33
}
Also available as Gist, if you have further questions.
I want to display calendar in this format
to the user. One option is to use "string range" to get the individual calendar components. The second one is to get it using NSCalendar which to me looks like the better one (is it?). So my code is as below. But there are two problems.
I am not getting the local time form "hour & minute components"
I am getting month in Int. I want it to be in String (month in mediumStyle)
Anyone know how to get what I need? Image attached is what exactly I want to achieve. There I am using three UILabel one for "date", second for "month, year" and third for "time".
Any help would be appreciated.
var inputDateString = "Jun/12/2015 02:05 Am +05:00"
override func viewDidLoad() {
super.viewDidLoad()
let newDate = dateformatterDateString(inputDateString)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: newDate!)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
println(newDate)
println(components)
println(day) // 12
println(month) // 6 -----> Want to have "Jun" here
println(year) // 2015
println(hour) // 2 ------> Want to have the hour in the inputString i.e. 02
println(minutes) // 35 ------> Want to have the minute in the inputString i.e. 05
}
func dateformatterDateString(dateString: String) -> NSDate? {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM/dd/yyyy hh:mm a Z"
// dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
return dateFormatter.dateFromString(dateString)
}
You can use DateFormatter as follow:
extension Formatter {
static let monthMedium: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "LLL"
return formatter
}()
static let hour12: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "h"
return formatter
}()
static let minute0x: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "mm"
return formatter
}()
static let amPM: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "a"
return formatter
}()
}
extension Date {
var monthMedium: String { return Formatter.monthMedium.string(from: self) }
var hour12: String { return Formatter.hour12.string(from: self) }
var minute0x: String { return Formatter.minute0x.string(from: self) }
var amPM: String { return Formatter.amPM.string(from: self) }
}
let date = Date()
let dateMonth = date.monthMedium // "May"
let dateHour = date.hour12 // "1"
let dateMinute = date.minute0x // "18"
let dateAmPm = date.amPM // "PM"
NSDateFormatter has monthSymbols, shortMonthSymbols and veryShortSymbols properties.
So try this:
let dateFormatter: NSDateFormatter = NSDateFormatter()
let months = dateFormatter.shortMonthSymbols
let monthSymbol = months[month-1] as! String // month - from your date components
println(monthSymbol)
I am adding three types. Have a look.
//Todays Date
let todayDate = NSDate()
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: todayDate)
var (year, month, date) = (components.year, components.month, components.day)
println("YEAR: \(year) MONTH: \(month) DATE: \(date)")
//Making a X mas Yr
let morningOfChristmasComponents = NSDateComponents()
morningOfChristmasComponents.year = 2014
morningOfChristmasComponents.month = 12
morningOfChristmasComponents.day = 25
morningOfChristmasComponents.hour = 7
morningOfChristmasComponents.minute = 0
morningOfChristmasComponents.second = 0
let morningOfChristmas = NSCalendar.currentCalendar().dateFromComponents(morningOfChristmasComponents)!
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .MediumStyle
let dateString = formatter.stringFromDate(morningOfChristmas)
print("dateString : \(dateString)")
//Current month - complete name
let dateFormatter: NSDateFormatter = NSDateFormatter()
let months = dateFormatter.monthSymbols
let monthSymbol = months[month-1] as! String
println("monthSymbol : \(monthSymbol)")
Print Results:
YEAR: 2015 MONTH: 10 DATE: 9
dateString : December 25, 2014 at 7:00:00 AM
monthSymbol : October
Update Swift 5.x Solution:
Today is Monday, 20 April, 2020
let date = Date() // get a current date instance
let dateFormatter = DateFormatter() // get a date formatter instance
let calendar = dateFormatter.calendar // get a calendar instance
Now you can get every index value of year, month, week, day everything what you want as follows:
let year = calendar?.component(.year, from: date) // Result: 2020
let month = calendar?.component(.month, from: date) // Result: 4
let week = calendar?.component(.weekOfMonth, from: date) // Result: 4
let day = calendar?.component(.day, from: date) // Result: 20
let weekday = calendar?.component(.weekday, from: date) // Result: 2
let weekdayOrdinal = calendar?.component(.weekdayOrdinal, from: date) // Result: 3
let weekOfYear = calendar?.component(.weekOfYear, from: date) // Result: 17
You can get an array of all month names like:
let monthsWithFullName = dateFormatter.monthSymbols // Result: ["January”, "February”, "March”, "April”, "May”, "June”, "July”, "August”, "September”, "October”, "November”, "December”]
let monthsWithShortName = dateFormatter.shortMonthSymbols // Result: ["Jan”, "Feb”, "Mar”, "Apr”, "May”, "Jun”, "Jul”, "Aug”, "Sep”, "Oct”, "Nov”, "Dec”]
You can format current date as you wish like:
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let todayWithTime = dateFormatter.string(from: date) // Result: "2020-04-20 06:17:29"
dateFormatter.dateFormat = "yyyy-MM-dd"
let onlyTodayDate = dateFormatter.string(from: date) // Result: "2020-04-20"
I think this is the most simpler and updated answer.
Swift 4.x Solution:
//if currentMonth = 1
DateFormatter().monthSymbols[currentMonth - 1]
Answer:
January
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let nameOfMonth = dateFormatter.string(from: now)
I cant find age in from birth date. What I got is
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
My code
override func viewDidLoad() {
super.viewDidLoad()
var dateString = user.birthday
var dateFormatter = NSDateFormatter()
// this is imporant - we set our input date format to match our input string
dateFormatter.dateFormat = "dd-MM-yyyy"
// voila!
var dateFromString = dateFormatter.dateFromString(dateString)
let age = calculateAge(dateFromString!)
}
func calculateAge (birthday: NSDate) -> NSInteger {
var userAge : NSInteger = 0
var calendar : NSCalendar = NSCalendar.currentCalendar()
var unitFlags : NSCalendarUnit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay
var dateComponentNow : NSDateComponents = calendar.components(unitFlags, fromDate: NSDate())
var dateComponentBirth : NSDateComponents = calendar.components(unitFlags, fromDate: birthday)
if ( (dateComponentNow.month < dateComponentBirth.month) ||
((dateComponentNow.month == dateComponentBirth.month) && (dateComponentNow.day < dateComponentBirth.day))
)
{
return dateComponentNow.year - dateComponentBirth.year - 1
}
else {
return dateComponentNow.year - dateComponentBirth.year
}
}
update: Xcode 11 • Swift 5.1
You can use the Calendar method dateComponents to calculate how many years from a specific date to today:
extension Date {
var age: Int { Calendar.current.dateComponents([.year], from: self, to: Date()).year! }
}
let dob = DateComponents(calendar: .current, year: 2000, month: 6, day: 30).date!
let age = dob.age // 19
Important:
The timezone must be set to create a UTC birth date otherwise there will be inconsistencies between timezones.
Swift 3
extension Date {
//An integer representation of age from the date object (read-only).
var age: Int {
get {
let now = Date()
let calendar = Calendar.current
let ageComponents = calendar.dateComponents([.year], from: self, to: now)
let age = ageComponents.year!
return age
}
}
init(year: Int, month: Int, day: Int) {
var dc = DateComponents()
dc.year = year
dc.month = month
dc.day = day
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(secondsFromGMT: 0)!
if let date = calendar.date(from: dc) {
self.init(timeInterval: 0, since: date)
} else {
fatalError("Date component values were invalid.")
}
}
}
Usage:
let dob = Date(year: 1975, month: 1, day: 1)
let age = dob.age
print(age)
In Swift 2.0+ age computing code should look something like this:
extension NSDate {
var age:Int {
return NSCalendar.currentCalendar()
.components(NSCalendarUnit.Year,
fromDate: self,
toDate: NSDate(),
options: NSCalendarOptions(rawValue: 0)
.year
}
}
Just use the DateTools pod. Absolutely the easiest way.
https://github.com/MatthewYork/DateTools
For Swift 3
import DateTools
let birthday: Date = ....
let ageString = String((Date() as NSDate).years(from: birthday))