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
Related
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");
I have got two TextBoxes(txtdateofissue and txtdateofexpiry).The First Textbox is Attached with a CalendarExtender control.
I want that if a user selects a date for the txtdateofissue textbox then automatically the same date but next year should get entered in txtdateofexpiry textbox.
Date Should be in dd-MM-yyyy format i.e. 24-04-2013.
i have written a code for this but i am getting an error (String is not a valid datetime format)
my code:
protected void txtdateofIssue_TextChanged(object sender, EventArgs e)
{
DateTime dt = Convert.ToDateTime(txtdateofIssue.Text);
dt = dt.AddDays(-1).AddYears(1);
txtdateofExpiry.Text = dt.ToShortDateString();
}
the first line in which i declare datetime variable dt throws up the error (String is not a valid datetime format)
when i run this code in my local machine it works fine but if i run it on iis then it shows errors...please either correct my code or guide me in a new way...thanks in advance...
you can use DateTime.TryParseExact method
DateTime dt;
if (DateTime.TryParseExact(txtdateofIssue.Text, "dd-MM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
dt = dt.AddDays(-1).AddYears(1);
txtdateofExpiry.Text = dt.ToString("dd-MM-yyyy");
}
To convert Date Time back to given format
Look at the docs for custom date and time format strings for more info.
The following case:
There is a string that has this format "2012-02-25 07:53:04"
But in the end, i rather want to end up with this format "25-02-2012 07:53:04"
I think i have 2 options. 1 would be to reformat the string and move it all around, but i dont think this is a clean way of doing this.
A other way that i was thinking about is to save the source string to a date parameter, and then write the date parameter back to a string in a certain date format.
But is this even possible to do ?
Do this:
DateTime.Parse("2012-02-25 07:53:04").ToString("dd-MM-yyyy hh:mm:ss");
Keep in mind this isn't culture-aware. And if you do need to store the intermediate result you could do that just as easily:
var myDate = DateTime.Parse("2012-02-25 07:53:04");
var myDateFormatted = myDate.ToString("dd-MM-yyyy hh:mm:ss");
Lastly, check out TryParse() if you can't guarantee the input format will always be valid.
Others have suggested using Parse - but I'd recommend using TryParseExact or ParseExact, also specifying the invariant culture unless you really want to use the current culture. For example:
string input = "2012-02-25 07:53:04";
DateTime dateTime;
if (!DateTime.TryParseExact(input, "yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime))
{
Console.WriteLine("Couldn't parse value");
}
else
{
string formatted = dateTime.ToString("dd-MM-yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
Console.WriteLine("Formatted to: {0}", formatted);
}
Alternatively using Noda Time:
string input = "2012-02-25 07:53:04";
// These can be private static readonly fields. They're thread-safe
var inputPattern = LocalDateTimePattern.CreateWithInvariantInfo("yyyy-MM-dd HH:mm:ss");
var outputPattern = LocalDateTimePattern.CreateWithInvariantInfo("dd-MM-yy HH:mm:ss");
var parsed = inputPattern.Parse(input);
if (!parsed.Success)
{
Console.WriteLine("Couldn't parse value");
}
else
{
string formatted = outputPattern.Format(parsed.Value);
Console.WriteLine("Formatted to: {0}", formatted);
}
Parse as DateTime then reformat it. Be careful: use always an IFormatProvider!
Yes, it is quite possible. All you need to do is use DateTime.Parse to parse the string into a DateTime struct and then use ToString() to write the date back out to another string with the format you want.
You can parse this as a date object and then provide the formatting you want when using the date.ToString method:
date.ToString("dd-MM-yyyy hh:mm:ss");
Yes, you can use custom DateTime format strings to parse and reformat DateTime objects.
DateTime date = DateTime.ParseExact("2012-02-25 07:53:04", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
string formattedDated = date.ToString("dd-MM-yyyy HH:mm:ss");
I use a model class with a DateTime value. I would like to display this property as two boxes, one for the date and the other one for the time.
What is the best way to do this? Any suggestions?
Thanks in advance!
Check this Splitting DateTime Blog post by Hanselman.
Format your datetime in 2 diffent properties.
first to retrieve datetime format as "yyyy/MM/dd"
second format as "HH:mm:ss"
All you need to do is use two format expressions, one to extract the time and the other to extract the date.
Bind this to the date box:
txtDateBox.Text = date.ToString("dd MMM yyyy");
Bind this to the time box:
txtTimeBox.Text = date..ToString("HH:mm:ss");
(Presumign your variable is called date).
You could do something like this (I didn't run the code, it's only an idea):
private DateTime MyModelDateTime;
public string date
{
get
{
return MyModelDateTime.ToString("MM/dd/yyyy");
}
set
{
string pattern = "MM/dd/yyyy HH:mm:ss";
string timeValue = MyModelDateTime.ToString("HH:mm:ss");
string dateTimeValue = value + " " +timeValue;
MyModelDateTime = DateTime.ParseExact(dateTimeValue, pattern, null, DateTimeStyles.None)
}
}
public string time
{
get
{
return MyModelDateTime.ToString("HH:mm:ss");
}
set
{
string pattern = "MM/dd/yyyy HH:mm:ss";
string dateValue = MyModelDateTime.ToString("MM/dd/yyyy");
string dataTimeValue = dateValue + " " + value;
MyModelDateTime = DateTime.ParseExact(dateTimeValue, pattern, null, DateTimeStyles.None)
}
}
in C# I'm trying to do a DateTime.TryParse on a string field which I am getting from a DataFeed which I know is in "MM/dd/YYYY" format. e.g.
DateTime dt = DateTime.TryParse("01/30/11");
Only thing is now "dt" is in the incorrect format to be stored into my Database which has a DateTime locale setting of "dd/MM/YYYY".
How do I go about parsing the string field correctly into dt and then into my DB? What would be the best way to handle this? If I set the globalization of my CurrentThread to en-US, then dt would be in en-US format, however when inserting into the DB, it is still incorrectly stored? :S
Thanks
David
Use the DateTime.ParseExact method:
DateTime dt = DateTime.ParseExact("15/12/2011", "dd/MM/yyyy", CultureInfo.InvariantCulture);
If you want to convert the DateTime value to a string back, use the following code:
string dtString = string.Format("{0: dd/MM/yyyy}", dt);
Try using TryParseExact.
http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx
I hope you understand, the TryParse be made by the if statement, see the following example,
private void button1_Click_1(object sender, EventArgs e)
{
DateTime myData = new DateTime();
if (DateTime.TryParse(this.textBox1.Text,out myData))
{
// your filed db = myData
}
}
you then enter in the block if the update of the database field, according to the cultures set on your PC.
Bye