How to change date format from DD/MM/YYYY or MM/DD

2020-01-27 07:09发布

i need to change the format of my date string using C#

from : "06/16/2010"or "16/06/2010"

to : "2010-06-16"

can you please help me achieve this

thanks

标签: c#
4条回答
够拽才男人
2楼-- · 2020-01-27 07:34

The following will do.

string datestring = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
查看更多
Bombasti
3楼-- · 2020-01-27 07:39

If you already have it as a DateTime, use:

string x = dt.ToString("yyyy-MM-dd");

See the MSDN documentation for more details. You can specify CultureInfo.InvariantCulture to enforce the use of Western digits etc. This is more important if you're using MMM for the month name and similar things, but it wouldn't be a bad idea to make it explicit:

string x = dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

If you have a string to start with, you'll need to parse it and then reformat... of course, that means you need to know the format of the original string.

查看更多
趁早两清
4楼-- · 2020-01-27 07:40

We can do something like this

DateTime date_temp_from = DateTime.Parse(from.Value); //from.value" is input by user (dd/MM/yyyy)
DateTime date_temp_to = DateTime.Parse(to.Value); //to.value" is input by user (dd/MM/yyyy)

string date_from = date_temp_from.ToString("yyyy/MM/dd HH:mm");
string date_to = date_temp_to.ToString("yyyy/MM/dd HH:mm");

Thank you

查看更多
冷血范
5楼-- · 2020-01-27 08:00
String dt = Date.Now.ToString("yyyy-MM-dd");

Now you got this for dt, 2010-09-09

查看更多
登录 后发表回答