Put time format in a Xamarin Forms input - c#

How about I would like to know how I can put a type of time format in an entry example 12:00 that when filling it there are the colons: that separate the hours from the minutes
I looked at some type of masks for the entrance but none adapted to what I want

If you are able to use the Xamarin CommunityToolkit, you could use a MaskedBehavior. In your case it would be something like this:
<Entry>
<Entry.Behaviors>
<xct:MaskedBehavior
Mask="xx:xx"
UnMaskedCharacter="x"/>
</Entry.Behaviors>
</Entry>

Related

convert String to different datetime formats

My input is an annoying free text in which I need to extract the date. This date could be present in any of the formats with anomalies.
eg. This is 9.9.12 date
This is 9912 date
This is 0992012 date
Any possible format.
dMy
ddMMyy
ddMMyyyy
Mdyy etc..
I am able to validate if the text is in date format for ddMMy* but not any other. I was looking into this https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1 link to see what other formats could be possible but I got no leads.
Is this correct to write like in the following method for a date format say - 9912?
Date.ParseExact(test.ToString,"dMy", Globalization.CultureInfo.InvariantCulture)
If yes, the system is populating me the following err:
System.FormatException: String was not recognized as a valid DateTime.
Can anyone please share a reliable approach to tackle such problems?
There is no single way to accomplish what you're trying to do. Specifically, because the intent is so broad, it becomes hard to infer what the user actually means (e.g. how do you differentiate month vs. day).
Depending on how you're implementing your application, you need to implement input validation.
For a console app, I can envision you using something like:
if (DateTime.TryParse(line, out value))
{
// Parse-able date.
}
else
{
// Non-parseable date.
}
If you're building a WPF application, you can use binding validation. If you're building an ASP.NET MVC application, you can also implement model validation. Your mileage may vary - you should definitely consider constraining what your users can feed the application.

issue with RazorEngine variable parsing

In my page, I'd like to display a string like this:
Today is (x)th of july
Where (x) is calculated by progam at runtime and passed as #Model.Number to RazorEngine
However the following template format will not work as I expected
Today is #Model.Numberth of july
In many cases I have to put variables and plain text together without spaces. How can I improve my code to make it work?
try using this
Today is #(Model.Number)th of july

Move to Following Date Part on Data Entry in DateTimePicker

I have a user request that I'm trying to accommodate, the simplest way I can think to explain is to use illustrate with a picture:
Essentially the user is typing a ton of dates in. Instead of typing the
MM [Backslash (Or Right Arrow)]
DD [Backslash]
YYYY [Backslash] etc ...
They'd like to Key the value and be moved to the next Date Part Value. While this doesn't seem like much I can imagine it adds up to allot of additional keystrokes and takes the end user off the number pad. I'm currently using Telerik's Winform RadDateTimePicker with Custom Format:
MM/dd/yyyy hh:mm tt
but would not be opposed to changing to the Winforms DateTimePicker.
Can anyone suggest an elegant solution for this ? The only thing I can currently think of is catching the keystrokes when the control is in focus but I feel like this could be messy.
Thankyou
RadMaskedEditBox (and respectively RadDateTimeEditor, which uses RadMaskedEditBox internally), have build in functionality to select the next part, however, it seems it does not work correctly with this mask, so I reported it as an issue. Here is the link to it: issue link. You can subscribe for status change alerts in order to get notified once its done.
What you can look at is the free form date time parsing introduced recently. It allows you to type in "10162014" which will get parsed to "10\16\2014". More information no parsing dates is available here: Parsing Dates
And the last thing would be to work with KeyDown and manually move the selection. There is API for this on the provider. Here is how to access it:
(this.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Provider as MaskDateTimeProvider).SelectNextEditableItem();
I hope some of this works for you.
This is not intended to be a link answer but I have to refer to the Telerik documentation to allow the OP to customize the control according to his needs.
My understanding is that your customer needs to type the date and time in a specific culture avoiding to type the separators.
You should use the maskedEditBox which already have a mask for your need MM/dd/yyyy hh:mm tt
Particularly you should set the pattern "s" which correspond to "Sortable date time pattern (based on ISO 8601) using local time"
Have a look at Telerik documentation here to set up your control as needed and the relevant validator.
This should solve your problem.
This is how it looks when empty:
and this si show it appears when filled in:
Using the relevant event (I have used radMaskedEditBox1_Leave) you can still use also a dateTimePicker taking the input from the radMasketTextBox:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM/yyyy HH:mm ss";
dateTimePicker1.Text = radMaskedEditBox1.Text;
Please check below URL fiddle URL for datetime mask:
1]. http://jsfiddle.net/jensbits/3yUes/light/
2]. http://jsfiddle.net/LkpPJ/5/

How to Convert date into MM/DD/YY format in C#

In My Asp.net webpage I need to display today's date into one of the textbox , so in my form load I wrote the following code
textbox1.text = System.DateTime.Today.ToShortDateString();
this line is giving me date like 1/7/09 but I want date like 01/07/09 , Is there anyway I can conver this date into mm/dd/yy format in C#?
DateTime.Today.ToString("MM/dd/yy")
Look at the docs for custom date and time format strings for more info.
(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)
DateTime.Today.ToString("MM/dd/yy")
Look at the docs for custom date and time format strings for more info.
(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)
Others cultures really are a problem. For example, that code in portugues returns someting like 01-01-01 instead of 01/01/01. I also don't undestand why...
To resolve that problem i do someting like this:
IFormatProvider yyyymmddFormat = new System.Globalization.CultureInfo(String.Empty, false);
return date.ToString("MM/dd/yy", yyyymmddFormat);
Have you tried the following?:
textbox1.text = System.DateTime.Today.ToString("MM/dd/yy");
Be aware that 2 digit years could be bad in the future...
Look into using the ToString() method with a specified format.
See, here you can get only date by passing a format string.
You can get a different date format as per your requirement as given below for current date:
DateTime.Now.ToString("M/d/yyyy");
Result : "9/1/2016"
DateTime.Now.ToString("M-d-yyyy");
Result : "9-1-2016"
DateTime.Now.ToString("yyyy-MM-dd");
Result : "2016-09-01"
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Result : "2016-09-01 09:20:10"
For more details take a look at MSDN reference for Custom Date and Time Format Strings

Parsing time without date in a TextBox

I'm working on a time sheet application, where I'd like the user to be able to enter times in TextBoxes, e.g.: 8 a or 8:00 a or the like, just like you can in Excel.
Now if you enter a date in a TextBox and then use DateTime.TryParse, you can enter it in several formats (Jan 31, 2007; 1/31/2007; 31/1/2007; January 31, 2007; etc.) and .NET will figure it out and turn it into a DateTime.
But when I use DateTime.TryParse on a string like "8 a" or "8:00 a", it doesn't understand it.
I know I can use ParseExact, but I'm wondering if there's a more flexible solution. I want .NET to get 8:00a from "8 a" or "8:00 a", and to leave the date component at the default 1/1/0001.
You can always cheat the system (quite easily):
DateTime blah = DateTime.Parse("1/1/0001 " + myTimeString);
I've done something similar myself.
I'd reconsider your UI functionality and replace it with two combo boxes, or some other form of time picker.
Ultimately, even the best time parser in the world will still fail the idiot user test.
Barring that, Parse.Exact is what you need.
Come to think of it, never mind. I just noticed that if I use "am" and "pm" instead of "a" and "p", it works fine. It assumes today's date, instead of the default 1/1/0001, but that's not a problem for my purposes.
(Still, any reasonably easy solution to get the "a" and "p" to work is welcome.)
You may be able to use the validating or validated event to capture the text and add the 'm'.

Categories

Resources