Is there a way to set the date format of the DatePicker (UIDatePicker)?
You can set the Locale and that would result in changing of the date format, but what if you would like to set a en_CA locale but instead of the default Jan 3, 2022 format you would want 2022-01-03.
My setup is in SwiftUI, but I guess the same approach would likely work for UIKit/SwiftUI.
DatePicker(
"My date",
selection: $date,
in: ...Date(),
displayedComponents: [.date]
)
.frame(alignment: .leading)
.labelsHidden()
.id(date) // hack, see https://stackoverflow.com/a/68120390/429763
.environment(\.locale, Locale.init(identifier: "en_CA"))
.onChange(of: date) { date in
Log("On change date: \(date)")
}
Related
I'm trying to filter values from a Column chart :
The chart is like this :
The lightgreen column chart value is from a Measure : Selection Date
And I use a Slicer to filter values like this :
The values are from a table Parametres
So here is my Selection_Date measure :
Selection Date = [DateFilter]
DateFilter is an other measure that choose how to filter the values (based on the slicer's selection).
DateFilter =
var _today = TODAY()
var _month = MONTH(_today)
var _year = YEAR(_today)
var _last_30_days = _today - 30
return SWITCH(SELECTEDVALUE(Parametres[Selection_date]),
"Last30Days", CALCULATE( SUM(v_revenues[qty]), FILTER(v_dates, v_dates[Date] > _last_30_days )),
"MonthToDate", CALCULATE(SUM(v_revenues[qty]), FILTER(v_dates, v_dates[actual_month] = 1)),
"YearToDate", CALCULATE(SUM(v_revenues[qty]), FILTER(v_dates, v_dates[actual_year] = 1))
)
This measure works but I still have one problem. The filter is ok but when I filter a values (example month to date) all the value that are not in the selection (all the values that are not november 2021) are still visible but they are empty :
So here is my question :
How can I add the values that are not in the selection ? How can I filter so it only display the values that are in the checked in the slicer ?
Per example, for Date to Month I want only this to be displayed :
I resolved it by using a Bookmark and then I associated a button to this Bookmark !
Help! Depending on the selected date, the displayed date for a Swift DatePicker with CompactDatePickerStyle shows the month ('Mar'):
Or shows the month number ('3'):
I want all dates to display with the month name (abbreviated or full).
Here's the complete application in Xcode version 12.4:
struct ContentView: View {
#State var date: Date = Date()
var body: some View {
DatePicker("Date", selection: $date)
.datePickerStyle(CompactDatePickerStyle())
.padding()
}
}
In this case, the output becomes 1 instead of 53 for the last week of the year, how can this be changed to show the proper week numbers?
struct ContentView: View {
#State var date = Date()
var weekFormatter: DateFormatter{
let formatter = DateFormatter()
formatter.dateFormat = "w"
return formatter
}
var body: some View {
VStack{
DatePicker(selection: $date, displayedComponents: .date, label: {Text("Bing")})
Text("\(date, formatter: weekFormatter)")
}
}
}
The meaning of the w character in the date format is described by Unicode Technical Standard #35 Part 4 ยง8.4. As of version 37, it says this:
8.4 Week of Year
Values calculated for the Week of Year field range from 1 to 53 for the Gregorian calendar (they may have different ranges for other calendars). Week 1 for a year is the first week that contains at least the specified minimum number of days from that year. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (if needed). For example, January 1, 1998 was a Thursday. If the first day of the week is MONDAY and the minimum days in a week is 4 (these are the values reflecting ISO 8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. However, if the first day of the week is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998. The first three days of 1998 are then part of week 53 of 1997.
Values are similarly calculated for the Week of Month.
So, if you want to change the way the weeks are numbered, you need to change the first day of the week, or the minimum days in a week, or both. You do that by creating a Calendar, setting the firstWeekday and minimumDaysInFirstWeek properties, and then assigning the Calendar to your DateFormatter's calendar property:
var calendar = Calendar(identifier: .gregorian)
calendar.firstWeekday = ...
calendar.minimumDaysInFirstWeek = ...
let formatter = DateFormatter()
formatter.calendar = calendar
formatter.dateFormat = "w"
Imagine I have a fact table with sales spanning 3 years (2016-2018). I have a chart showing sales by month (36 points on the X-Axis). I have a slicer selection to Year = 2018, and Month = June.
Is it possible, with a measure, to show on a chart, the trailing 6 months from the slicer selection? In other words, with the slicer still set to Year = 2018 and Month = January, can the chart display 6 points (the trailing 6 months)?
How would this be accomplished?
The approach I would use in this case would be to create a parameter table for the date which doesn't have a relationship with my other tables and use that date for the slicer. Then you'd write the sales measure you use on the chart to read the selected date and return blanks for any dates not within the range you want.
Roughly like this:
NewSalesMeasure =
VAR SelectedDate = SELECTEDVALUE(Slicer[Date])
VAR CurrentDate = SELECTEDVALUE(Sales[Date])
RETURN IF(CurrentDate <= SelectedDate &&
CurrentDate > DATEADD(SelectedDate, -6, MONTH)
SUM(Sales[Amount]),
BLANK()
)
How to set the first date of the week to Wednesday and show the first and last date of week on yellow bar? Because the schedule will update every Wednesday.
Please check this might help :
var calendar = Calendar.current
calendar.firstWeekday = 4
let startOfWeek = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))!
let endOfWeek = calendar.date(byAdding: .day, value: 6, to: startOfWeek)!
print(startOfWeek)
print(endOfWeek)