开发者

How can I convert a date from the format "dd/mm/yyyy" to "yyyy/dd/mm"?

开发者 https://www.devze.com 2023-02-01 02:13 出处:网络
I have a Textbox in which the user is supposed to enter a date in the format dd/mm/yyyy, this date is stored as yyyy/mm/dd in the database.

I have a Textbox in which the user is supposed to enter a date in the format dd/mm/yyyy, this date is stored as yyyy/mm/dd in the database.

So I want the user to enter the date in dd/mm/yyyy forma开发者_如何学JAVAt and later I want to convert to yyyy/mm/dd so that I can query database.

How can I convert the user input date dd/mm/yyyy to yyyy/mm/dd?


Quick and maybe dirty:

string reformattedDate = DateTime.ParseExact(textBox1.Text, "dd/MM/yyyy", null).ToString("yyyy/MM/dd");

This, of course, assumes that the content of the Textbox will always be in the assumed format, so their should be checks in before to ensure that. There's also the much safer way which does not need additional checks:

DateTime result;
if(DateTime.TryParseExact(textBox1.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out result)
{
    // Here you can safely use result
    string reformattedDate = result.ToString("yyyy/MM/dd");
} else {
    // Screw the user
}

This will try to parse the content of the Textbox and return it to your via the out result, if successful, it will return true. Otherwise you can rest assured that the input was either not in the assumed format, or not a valid Date.

Please also be aware that ParseExact will throw an ArgumentNullException or an ArgumentException if the conversion fails.

  • MSDN Article for ParseExact
  • MSDN Article for TryParseExact

I have a text box in which the user is supposed to enter a date in dd/mm/yyyy format, this date is stored as yyyy/mm/dd in the database.

The obvious answer to this is: use a DateTimePicker and a parameterized query before wrestling with a string conversion. It will save you a lot of headache in the long run.


If you store a date in the database, then store it as a date not as a string. Then you will only have one format: what the user typed in.

Using DateTime.Parse (or TryParse, or the "Exact" versions) you can convert from the typed-in string to a DateTime value.


I would have a look at

DateTime.TryParseExact Method

and

DateTime.ToString Method Somthing like dateTimeval.ToString("yyyy/MM/dd");

string dateString = "30/01/2010";
DateTime dateTimeVal;
if (DateTime.TryParseExact(dateString,"dd/MM/yyyy", null,DateTimeStyles.None,out dateTimeVal))
{
    string newFormat = dateTimeVal.ToString("yyyy/MM/dd");
}


this date is stored as yyyy/mm/dd in the database

I hope that this is not true. I hope that you store the date as a datetime value in the database and not as a varchar value.

If you store the date as a datetime value, there is no point in converting the date string from one format to another, you should parse the string to a DateTime value.

Use the ParseExact method to parse a string from a specific format:

DateTime date = DateTime.ParseExact(theDateTextbox.Text, "dd'/'MM'/'yyyy", CultureInfo.InvariantCulture);


Here's the way to convert a DateTime to a string:

dateTime.ToString("dddd, dd MMMM yyyy");

Here are the different format of ToString

But I think it is better to use a DateTimePicker, you're sure to have a date and user can use keyboard to input a date.


string input = "20/12/2010"; // dd/mm/yyyy
DateTime d;
if (DateTime.TryParseExact(input, "dd/mm/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d))
{
    string result = d.ToString("yyyy/mm/dd");
}


Assuming you store the date in a DateTime field in the database, the date is not actually stored in the format dd/mm/yyyy.

To render the date use:

string formattedDate = dateFromDb.ToString("yyyy/MM/dd");

To convert a date string for a particular format you can use:

DateTime value;

if (DateTime.TryParseExact(
  date,
  "dd/MM/yyyy", 
  new CultureInfo("en-GB"),
  System.Globalization.DateTimeStyles.None,
  out value))
{
  Console.Write(value.ToString("yyyy/MM/dd"));
}
else
{
  Console.Write("Date parse failed!");
}


Try this.

 string dt = DateTime.Parse(txtDate.Text.Trim()).ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
0

精彩评论

暂无评论...
验证码 换一张
取 消