I am looking to change the format of the selected date in the label of this SwiftUI DatePicker:
Currently, it is formatted as dd/m/yy + time, but I need it formatted with the name of the month, as dd MMMM yyyy + time (e.g. 30 June 2020 9:00am).
This is how the default calendar app displays it, which is how I want it:
I can't seem to find any methods within DatePicker to be able to do this.
Current code:
DatePicker("Starts", selection: self.$request.startDate.onChange({_ in ...}))
Because I assume you want to use the DatePicker within a Form, I have to say that this is not possible. Because DatePicker doesn't give you access to the actual date Label.
If you use it outside a form you could reference to your date and format it with DateFormatter as you like it to be.
Related
The value I want from another google sheet is
May 30, 2022
I want to add a text string to this
" Time Sheet"
I am using this:
='TimeSht 5/30 - 6/12'!K5&" Time Sheet"
I get this
44711 Time Sheet
TEXT() is your friend
A date is technically a number formatted as a string. Use TEXT to make the illusion of a string into a reality.
=TEXT('TimeSht 5/30 - 6/12'!K5, "MMM dd, YYYY")&" Time Sheet"
Trying to do something which seems like it should be simple:
I have a small gui application in Google scripts. I would like to make a datePicker object appear, with the value (starting date) of a cell in the spreadsheet which the app works. I have looked through many of similar questions on stackOverFlow, but still can not find an answer that works.
For example, cell (2,5) in my sheet has a date in it, formated by Google itself. I would like a datePicker to display that date. Here is where I am at.
function testRun()
{
var ss = SpreadsheetApp.getActive();
var ui = UiApp.createApplication();
var controls = ui.createVerticalPanel().setHeight(350).setWidth(350);
var sheet = ss.getActiveSheet();
//Here I grab a cell's data. It logs as a date: Sat May 02 00:00:00 GMT-05:00 2015
var lastDay = sheet.getRange(2, 5).getValue();
Logger.log(lastDay);
//Here I would like to display a datePicker with the date
var datePicker = ui.createDatePicker();
datePicker.setValue(new Date(lastDay));
//datePicker.setvalue(lastDay); also does not work.
controls.add(datePicker);
To summarize: I can't get this datePicker to display a different day, based on a google sheet cell.
Thanks for the help.
The date picker has a setValue() method so that you should be able to set its value using a normal date object... but is seems to be broken. The dateBox has the same method but it works as expected.
That's one for the issue tracker I'm afraid. (added just now : issue 4282 but UiApp won't be updated anymore so I guess this bug will stay there forever...)
demo code :
function doGet() {
var app = UiApp.createApplication();
app.add(app.createDateBox().setValue(new Date(1958,1,19,2,0,0,0)))
app.add(app.createDatePicker().setValue(new Date(1958,1,19,2,0,0,0)))
return app;
}
demo app online
In this example I set the date in the script to february 19, 1958 but a date read from a spreadsheet would work as well of course.
PS : this is my birthday date, I know I'm old ;-)
I am showing the date field in the UILabel the problem is when i change the setting of ipad
using
settings->international->calender
and change it to Buddhist from gregorian the date format differs.
How do i fix this issue so that it show the date format in gregorian only even when calender is changed?
The default UIDatePicker shows both the date and time. I need to show only the month, day, year and time like this
How can I change the date picker to this?
Well First off I suggest reading through the UIDatePicker documentation. Here are the Date Picker Modes:
Date Picker Mode
The mode of the date picker.
typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
} UIDatePickerMode;
If you cannot find what you need there. Then you can create your own by using a UIPickerView.
My UIDatePicker is set to UIDatePickerModeTime but on the apple documentation it says The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].. How do i add AM/PM because on my device, those aren't options and it instead uses a 24 hour clock?
This will depend on the settings of the user. If you go to Settings -> General -> Date & Time. there is a "24-Hour" time switch, and based on that users will have the AM/PM indicator on your picker.
Unfortunately UIDatePicker doesn't have an option for that; it always respects the device option of 12-hour or 24-hour time.
If you must have an AM/PM component you may have to go with a custom UIPickerView instead, and build an NSDate out of its components manually.
This thread is somewhat old, but maybe this will be useful to someone else...
As written in the original question, the picker will respect the locale. So, if you what to overrule the system-settings you can set the desired locale on the picker like this:
picker.locale = Locale.init(identifier: "en")
Two things to check,
UIDatePicker has mode which needs to be either set to .time or .dateAndTime in order to support AM/PM format.
set locale to a locale that supports AM/PM, eg "en_US_POSIX", if your locale is set to current, it will read system locale.