Display Year, Month and Date in sequence on DateTimePicker iOS - ios

Here is my situation, Current Date picker is showing date, month and year in sequence of Date, Month and Year OR Month, Date and Year. But I want to display this sequence as Year first then Month and at last Date.
I searched a lot but same question I found in Javascript but not in iOS, I also searched in Apple Documentation but I didn't find any solution.
How can I achieve this sequence Year, Month and Date? If there any property or method available for DateTime Picker? OR Do I need to use custom picker? if any then which?
Thanks.

The order of year, month and day is depending on the picker's locale.
Eg.
datePicker.locale = NSLocale(localeIdentifier: "zh_TW")
will have different order from
datePicker.locale = NSLocale(localeIdentifier: "en_US")

Related

Setting only specific months in UIDatePicker Swift 5

In my project i want to apply condition like, user can select date only 3months from current date.
I.e i want to display a date picker containing Month and Day with range of 90 days.
Eg. November is going on so only, November December January should be displayed in Date picker for picking.
How can i achieve that??
You can try something like this:
datePicker.maximumDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
UIDatePicker has minimumDate and maximumDate properties. Use those to set the range of dates you want to allow the user to enter.
Edit:
#wonder posted code to calculate a date 3 months in the future:
futureDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
Alternately, you could add 90 days to the current date if that's what you want:
futureDate = Calendar.current.date(byAdding: .day, value: 90, to: Date())
Set the datePickerMode to .date to have the user choose only dates, not times.
I don't know how to remove the year selector, although if you select a date range of 90 days that all fall in the same year, they won't be able to select a year other than the current year.
(Note that starting in October, a date range of 90 days includes dates in 2 different years, so you would need the year anyway.)
As wonder said in a comment, you could create your own month and day picker using a UIPickerView, but you would need to add logic to change the number of days based on the month, and exclude days of the current month that are in the past.

Swift: first day of the week in app doesn't change after I change it in Settings

In my app I need to group different items based on their date, so I need a method to get date of the beginning of the week, and also a method to find out if two dates are in the same week. However, I get unexpected problems trying to implement this.
I use this code to get the first day of the week:
return Calendar.current.date(from: Calendar.current.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
I use Calendar.current, but it returns the same day no matter which day is chosen as the beginning of the week in iOS Settings.
To check if two dates are in the same week I use this code:
func isInSameWeek(date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
Still, Sunday and Monday dates are always considered not in the same week no matter which day is chosen as the beginning of the week in iOS Settings.
How do I fix this? Or maybe it is a normal behavior and this code will work correctly for users in different regions?
You are changing the settings of the Calendar App not the iOS current calendar itself. You need to provide your own app settings to be changed by the user.
var calendar = Calendar.current
calendar.firstWeekday = 2
calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))?.description(with: .current) // "Monday, September 14, 2020 at 12:00:00 AM Brasilia Standard Time"

Problem Converting Fiscal Year to Calendar Dates

I need help using a query to convert Fiscal year to Calendar Year. For Example, I have the Fiscal_Year column and Fiscal_Quarter Column, as shown below
Fiscal_Year Fiscal_Qtr Calendar_Year
FY21 4Q
FY22 4Q
FY21 2Q
FY24 4Q
FY24 2Q
I need help coming up with a query of converting the Fiscal year to normal calendar dates group of Month/Date/Year(mm/dd/yyyy). if it is Fiscal_Year FY21 and Fiscal_Qtr is 4Q, then the date should be 9/30/2021, and if it is Fiscal_Year FY21 and Fiscal_Qtr is 2Q then the Calendar_Year should be 06/30/2021

Comparing timestamps and working with timezones on iOS

Given a unix timestamp, how can my iOS code know if it is before or after 6am (6am = the most recent 6am that occurred)? Are there time zones involved?
Convert the unix timestamp to a Date. Then use Calendar to get the DateComponents from the Date. By default, these components will be interpreted in the user's current timezone. If you wish to interpret the date in a different timezone, set the calendar's timezone before getting the components from the date.
By looking at the desired components you can make your decision about the hour.
let date = Date(timeIntervalSince1970: someUnixTimeStamp)
let components = Calendar.current.dateComponents(in: TimeZone.current, from: date)
// look at hour as needed
There are other Calendar APIs if you just want a single component or just a smaller subset of components instead of all components from the date.

Retrieve current date without leading zeroes

I am trying to compare the date chosen on a calendar (Kal calendar implementation) with the current date. The issue is that the current date is in MM/dd/yyyy format, whereas the Kal dates will have no leading zeroes if the month or day is below 10. Is there an easy way to retrieve the current date so that it will not have leading zeroes for the day or month if it's under 10? (The current date will actually be utilized as an attribute for saved objects, so that they can later be queried using the date selected with Kal)
example current date - 07/07/2014
example Kal date - 7/7/2014
Don't compare strings holding dates. Create actual NSDate objects, using an NSDateFormatter, and compare those. The format strings you need are "MM/dd/yyyy" for your date, however you're retrieving it, and "M/d/yyyy" for Kal's date.
I have used en_US_POSIX along with "MM/dd/yyyy" & it's Working Fine
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MM/dd/yyyy"
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7

Resources