dateTime to short format in winform - c#

I want to display date time like 01/08/11 , but i write this
string title_row = DateTime.Now.ToShortDateString();
and the result is 8/1/2011 how i can change the formt to 01/08/11

How about?
DateTime.Now.ToString("dd/MM/yy")
, alternatively you should investigate Culture.

You can use the string format parameter of the ToString method to achieve any format you like, like this:
string title_row = DateTime.Now.ToString("dd/MM/yy");
//01/08/11

For example using DateTime.Now.ToString("dd/MM/yyyy)
Check http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

DateTime.Now.ToString("dd/MM/yy"); should get you the required format
ToShortDateString() is governed by the culture settings and the display format is derived from the current culture, so to get it in the desired format pass in the appropriate formatting.

Related

How to change format of Date into the form 02-Jun-2015

I am filling my Grid from database. On RowEditing event I am fetching data from grid to TextBox. This is the code that I am using
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"]).ToShortDateString();
This gives me date in 02-06-2015 format but I want to display date in 02-Jun-2015 format.
You could use the ToString method and pass there the format you want.
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"]).ToString("dd-MMM-yyyy");
At the bottom of this, you will find also other useful formats.
From DateTime.ToShortDateString method
The value of the current DateTime object is formatted using the
pattern defined by the DateTimeFormatInfo.ShortDatePattern property
associated with the current thread culture.
That means, your CurrentCulture's ShortDatePattern is dd-MM-yyyy. If you want abbreviated month name of your CurrentCulture, you can use MMM specifier instead.
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"])
.ToString("dd-MMM-yyyy");
Remember, if your CurrentCulture is not english-based one, this MMM specifier will generate a different name than Jun. In such a case, you can provider an english-based IFormatProvider (like InvariantCulture) as a second parameter in ToString method like;
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"])
.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture);
To get the DateTime format, box the string into DateTime and use the the formatting string via ToString(), like :
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"]).ToString("dd-MMM-yyyy");
visit this link of MSDN for complete details:
https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
Simply use this
Convert.ToDateTime(YourDataSet.Tables[0].Rows[0]["Date"]).ToString("dd MMM yyyy");
OR
Convert.ToDateTime(YourDataSet.Tables[0].Rows[0]["Date"]).ToString("dd/MMM/yyyy");
Or what ever you want
Like this:
txtDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["BuyDate"]).ToString("dd MMM yyyy");
See here: examples how to format DateTimes.
In your case dd-MMM-yyyy wold be the correct pattern.
As mentioned, you don't have to use string.Format(), the .ToString() does also allow to pass the pattern.
Please note, that you should also consider to keep the information as DateTime (not string) and format it in a Binding (if you are using DataBinding) that would sepparate the view from the underlying data

Formatting DateTime - ignore culture

I need to format a date to the following format:
M-d-yyyy
I tried using:
string.Format("{0:M-d-yyyy}", DateTime.Now)
But the output string will depend on the CurrentCulture on the computer where it's run, so sometimes the output might be 07/09/2014 or 07.09.2014 instead of 09-07-2014.
How can I easily prevent it from converting it based on the culture and treating it as a literal string?
Use CultureInfo.InvariantCulture as the culture or provider argument.
String.Format(CultureInfo.InvariantCulture, "{0:M-d-yyyy}", DateTime.Now)
Use CultureInfo.InvariantCulture as an IFormatProvider parameter:
DateTime.Now.ToString("M-d-yyyy", CultureInfo.InvariantCulture);
You can set the culture of your program with this:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;`
You can also use a specific culture if you want (I think en-US is the one you need)
Use the following:
DateTime.Now.ToString("d", DateTimeFormatInfo.InvariantInfo);
or apply other formatting specs as detailed in http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx
Pertinent to your case it could be written as:
DateTime.Now.ToString("M-d-yyyy", DateTimeFormatInfo.InvariantInfo);
Regards,
You can use the .ToString() method on the DateTime object to format it however you'd like. Your code would look something like this:
DateTime.Now.ToString("M-d-yyyy");
More info on formatting date times can be found on the MSDN: http://msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.110%29.aspx
you can try
date.ToString("MM/dd/yy", yyyymmddFormat);
or
try whats in this link
http://social.msdn.microsoft.com/Forums/en-US/af4f5a1e-f81d-47fe-981d-818e785b8847/convert-string-to-datetime-object
you can force the string into a standard format if you like

Convert DateTime to US is not good

I have a DateTimePicker in WPF.
The dateTimePicker give me an European Format
In my DateTimePicker, i select "01/02/2014" ( in european format )
I would like to read the value, and convert it in a shortDateString with the US culture.
I have done this :
CultureInfo m_UsCulture = new CultureInfo("en-us");
string str = m_dDate.Date.ToShortDateString().ToString(m_UsCulture);
The str variable is : "01/02/2014" instead of "2/1/2014"
The ShortDateString appear to be OK, but not the "ToString(m_UsCulture);".
I would like to do this in a one line please. Have you an idea for this error ?
Thanks a lot :)
The dateTimePicker give me an European Format
That could only be true if you're using the Text property. I suggest you use the SelectedDate property, which will give you a DateTime? instead - and a DateTime doesn't have a format... it's up to you to format it however you want.
Then to convert them to US short date format, you should use ToString and specify d (for short date format) and the culture at the same time:
string str = m_dDate.Date.ToString("d", m_UsCulture);
Your current code is using DateTime.ToShortDateString() which will use the current culture, and then calling ToString on the resulting string which won't care about the culture you pass it.
ToShortDateString uses the current culture, so you can't specify your US culture with this method (unless you change the current culture). I would use ToString instead, and the US-specific short date pattern:
CultureInfo usCulture = CultureInfo.GetCultureInfo("en-us");
var stringRep = yourDate.ToString(
usCulture.DateTimeFormat.ShortDatePattern,
usCulture);

how to conver date string from one format to another format in c#?

Suppose I have date string like mydate = "24-Jun-2011";
I want to convert it to another format "2011-06-24".
What is the simple way to do this?
The best way is to parse the string to a DateTime and then convert it to a string again.
Be sure to have a look at the documentation for DateTime.Parse, DateTime.TryParse and DateTime.ToString
DateTime.Parse(myDate).ToString("yyyy-MM-dd");
DateTime.ParseExact("24-Jun-2011", "dd-MMM-yyyy").ToString ("yyyy-MM-dd")
See formats here at MSDN.
U can Parse it to DateTime and then using tostring + special format get what u need
http://www.csharp-examples.net/string-format-datetime/
has a lot of different formatting options... This should work well for you.

Parsing exact dates in C# shouldn't force you to create an IFormatProvider

Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific formats) dates in C# should be as easy as
DateTime.ParseExact(theDate, "yyyy/MM/dd");
but no, C# forces you to create an IFormatProvider.
Is there an app.config friendly way of setting this so I don't need to do this each time?
DateTime.ParseExact(theDate, "yyyy/MM/dd", new CultureInfo("en-CA", true));
The IFormatProvider argument can be null.
ParseExact needs a culture : consider "yyyy MMM dd". MMM will be a localized month name that uses the current culture.
Use the current application culture:
DateTime.ParseExact("2008/12/05", "yyyy/MM/dd", System.Globalization.CultureInfo.CurrentCulture);
You can set the application culture in the app.config using the Globalization tag. I think.
Create an extension method:
public static DateTime ParseExactDateTime(this string dateString, string formatString) {
return DateTime.ParseExact(dateString, formatString, new CultureInfo("en-CA", true));
}
It requires the format provider in order to determine the particular date and time symbols and strings (such as names of the days of the week in a particular language). You can use a null, in which case the CultureInfo object that corresponds to the current culture is used.
If you don't want to have to specify it each time, create an extension method which either passes null or CultureInfo("en-CA", true) as the format provider.
You could also simply create the IFormatProvider once and store it for later use.
You could also use the Convert class
Convert.ToDateTime("2008/11/25");
//Convert date to MySql compatible format
DateTime DateValue = Convert.ToDateTime(datetimepicker.text);
string datevalue = DateValue.ToString("yyyy-MM-dd");
What's wrong with using Globalization.CultureInfo.InvariantCulture ?

Categories