how to change time picker format 12hr in xamarin forms? - c#

when i am trying to print in label time format would be change to 24 hr i need 12 hr.
trying to change the output format of time picker but I couldn't solve.
here is my code
Xaml:
<TimePicker x:Name="timepic" Format="hh:mm:tt"></TimePicker>
<Label x:Name="time"/>
cs
time.Text = timepic.Time.ToString();

by default, ToString() will format it using whatever your locale settings are. If you want to override that, provide a format specifier
time.Text = timepic.Time.ToString("hh:mm:tt");

To convert DateTime objects into 12h or 24h, you can use these methods.
public static string ToFormat12h(this DateTime dt)
{
return dt.ToString("yyyy/MM/dd, hh:mm:ss tt");
}
public static string ToFormat24h(this DateTime dt)
{
return dt.ToString("yyyy/MM/dd, HH:mm:ss");
}
So, in your case:
time.Text = timepic.Time.ToString("hh:mm:ss tt");

Related

How to input a formatted date in a text box?

I'm confuse about how to make an input of formatted date time and currency. I want user to input the DoB as dd/mm/yyyy but when I'm using DateTime data type in Visual Studio it only get yyyy/mm/dd format.
Here's my code:
This is DoB and property from another class employee.cs
class employee
{
private DateTime myBOD;
public DateTime BOD
{
get
{
return myBOD;
}
set
{
myBOD = value;
}
}
}
This is the main form1.cs
vemployee.BOD = Convert.ToDateTime(bod.Text);
var today = DateTime.Today;
age.Text = Convert.ToString(today.Year-vemployee.BOD.Year);
Well, DateTime is a struct it doesn't have any format but properties like Year, Month, Day etc.
use DateTime.ParseExact when you want to obtain DateTime from string:
vemployee.BOD = DateTime.ParseExact(
bod.Text,
"dd'/'MM'/'yyyy", // Please, note that "mm" stands for minutes, not months
CultureInfo.InvariantCulture);
And .ToString(format) when you want to represent DateTime as a string
DateTime today = DateTime.Today;
bod.Text = today.ToString("dd'/'MM'/'yyyy", CultureInfo.InvariantCulture);

How to show date in diffrent way in GridView?

I'd like to change DisplayFormat in my grid from
"MM/dd/yyyy"
to
"yyyy/MM/dd hh:mm:ss"
but nothing happens.
You should use this (example):
static void Main()
{
DateTime time = DateTime.Now; // Use current time.
string format = "yyyy/MM/dd hh:mm:ss"; // Use this format.
Console.WriteLine(time.ToString(format)); // Write to console.
}
So when inserting your data in the GridView, you convert it to a string with the selected format.
Here's the link for multiple date format and how to use them: https://www.dotnetperls.com/datetime-format
It is easier for me to answer my question than isolated working code in my complex program. So this is my hack that works inside my program:
private void GridView_CustomDrawEvent(object sender, RowCellCustomDrawEventArgs e)
{
if(e.Column.FieldName == "CreationDate")
{
string date = ((DateTime) view.GetRowCellValue(e.RowHandle, "CreationDate"));
string displayDate = date.ToShortDateString() + " " time.ToLongTimeString();
e.DisplayText = displayDate;
}
}
I catch my DateTime object in CustomDrawCell event and I am translating it to string using two standard methods DateTime.ToShortDateString() and DateTime.ToLongTimeString(). Thanks very much for your time.
Settings
Set Display format
dd-MMM-yyyy hh:mm:ss tt
Result

Convert US Date To UK Date Format C#

All,
I'm really new to C# after years of VBA Programming. I have made an attempt at trying to convert a US date which is passed into the script via a parameter into a UK date. (Apologies if this is a really basic question - I am still working through training materials)
The issue I am having is passing the UK date out as a parameter.
Code below:
using System.Globalization;
namespace Dynamic.Script_8D643DCA79B170B
{
// Please use caution when modifying class name, namespace or attributes
[OpenSpan.TypeManagement.DynamicTypeAttribute()]
[OpenSpan.Design.ComponentIdentityAttribute("Script-8D643DCA79B170B")]
public sealed class Program
{
public void Dateformat(Datetime USdate, Datetime UKdate)
{
string value = USdate;
string format = "dd/MM/yyyy";
DateTime result;
if (DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
Console.WriteLine(result.ToString(format));
}
else
{
Console.WriteLine("Date invalid");
}
}
}
}
Essentially I would like to pass a date formatted as MM/dd/YYYY into the script and convert it into UK date format dd/MM/YYYY as a variable. Any help and guidance on how to adapt the above script would be much appreciated.
*****Edit**********
Thanks for all your help and advice so far, I have been able to develop a new script see below:
public string Dateformat(string USdate)
{
string date = USdate; // en-US formatted date
// Convert to DateTime
DateTime parsedDate = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
// Output in en-GB format
return parsedDate.ToString("dd/MM/yyyy");
}
The issue I am having with the above script is that the string was not recognized as a valid DateTime. See screenshot:
The USdate I am passing into the script is formatted in DateTime.

Convert 12 hour time to Timespan C#

Using ASP.NET Forms, I'm encountering a problem with converting a 12 hour time into a timespan. Below I'm combining DateTime with TimeSpan as the user chooses a date and then a time. The fields are controlled by javascript.
DateTime DateResult = DateTime.TryParse(txtDate.Text, out DateResult) ? DateResult : DateTime.Today;
TimeSpan TimeResult = TimeSpan.TryParseExact(txtTime.Text, "h:mm tt", CultureInfo.InvariantCulture, out TimeResult) ? TimeResult : new TimeSpan();
DateResult = DateResult.Add(TimeResult)
So parsing the date works fine, but Timespan doesn't. One example:
Date Entered: 08/03/2018
Time Entered: 3:00 AM
Values are gettined passed okay but time fails so DateResult becomes "08/03/2018 00:00" but not "08/03/2018 03:00". I have also tried using the method TimeSpan.TryParse but no luck with that one.
I've also made sure that the format is correct by manually entering the time in the database behind the scenes. The gridview has a column that shows the full date in this format "dd/MM/yyyy h:mm tt", and works.
Anyone please share some light? Ideally, I would like to avoid any third party plug-ins.
Parse them together
Simplest thing is to just concatenate the strings before parsing as a single DateTime, e.g.
var dateEntered = #"08/03/2018";
var timeEntered = #"3:00 am";
DateTime result;
var completeDateString = dateEntered + " " + timeEntered;
var ok = DateTime.TryParse(completeDateString, out result);
if (!ok) result = DateTime.Today;
Console.WriteLine(result);
Output:
8/3/2018 3:00:00 AM
Ta da
If you have to parse them separately
If you'd like to work with the fields separately, you still can (I guess you'd have to do this if you want the time format to be exact but the date portion to be flexible, as it is in your example). But TimeSpan.TryParseExact is really different from DateTime.Parse. The format codes are different; it doesn't support the ":" character (except as a literal with an escape, e.g. "\:"), for example, or the "tt" formatting specifier. I'm guessing the concept of am/pm has to do with an absolute point in time, not a relative time offset, so isn't provided for. But you can still parse the textbox as a DateTime and use its time portion.
You can probably shorten this a bit but this example gives you everything you need:
static public DateTime ParseDateTime(string input)
{
DateTime output;
var ok = DateTime.TryParse(input, out output);
if (ok) return output;
return DateTime.Today;
}
static public TimeSpan ParseTime(string input)
{
DateTime output;
var ok = DateTime.TryParseExact(input, #"h:mm tt", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out output);
return output.Subtract(output.Date);
}
public static void Main()
{
var dateEntered = #"08/03/2018";
var timeEntered = #"3:00 am";
DateTime dateResult = ParseDateTime(dateEntered);
TimeSpan timeResult = ParseTime(timeEntered);
DateTime finalResult = dateResult.Add(timeResult);
Console.WriteLine(finalResult);
}
Output:
8/3/2018 3:00:00 AM
Code on DotNetFiddle
See ParseExact or https://msdn.microsoft.com/en-us/library/system.timespan.tryparseexact(v=vs.110).aspx for TryParseExact should work for both DateTime as well as TimeSpan inter alia
Fyi it's called the meridian and see also AM/PM to TimeSpan

How to Convert Date Object with Format "MM/dd/yy hh:mm:ss tt" to DateObject with Format "dd/MM/yy

I have googled alot and tried lot of solutions but nothing is working for me.For Ex i Have tried below :
public static DateTime ParseDateToSystemFormat(DateTime date)
{
IFormatProvider culture = new CultureInfo("en-GB", true);
DateTime dt = DateTime.ParseExact(date.ToString("dd/MM/yyyy"),
"dd/MM/yyyy",
culture,DateTimeStyles.NoCurrentDateDefault);
return Convert.ToDateTime(dt,culture);
}
If anyone have solved this please let me know.
Date objects do not have formatting associated to them - you only use formatting for display.
When it is time to display the DateTime object, use either custom or standard format strings to format the display to your liking.
What you are doing here:
DateTime dt = DateTime.ParseExact(date.ToString("dd/MM/yyyy"),
"dd/MM/yyyy",
culture,DateTimeStyles.NoCurrentDateDefault);
Is rather strange - you are getting a specific string representation of your DateTime - date.ToString("dd/MM/yyyy"), then parsing that string back to a DateTime object. A bit of a long way to say DateTime dt = date;, with clearing out the hours/minutes/seconds data.
If you simply want the date portion of a DateTime, use the Date property. It produces:
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
The internal representation of a DateTime is always the same. There is no formatting attached to a DateTime object.
If it is only a display problem, then convert the DateTime to a string and display that string. You already know how to do it: Using ToString and specifying the format you want to have.

Categories

Resources