将日期时间转换为日期格式dd / mm / yyyy

时间:2023-01-02 09:31:39

i have an object 2/19/2011 12:00:00 AM. i want to convert this object to 19/2/2011. please give me the most possible answers

我有一个对象2/19/2011 12:00:00 AM。我想将此对象转换为19/2/2011。请给我最可能的答案

convert datetime to string format.

将datetime转换为字符串格式。

12 个解决方案

#1


74  

DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

#2


18  

First of all, you don't convert a DateTime object to some format, you display it in some format.

首先,您不将DateTime对象转换为某种格式,而是以某种格式显示它。

Given an instance of a DateTime object, you can get a formatted string in that way like this:

给定一个DateTime对象的实例,您可以像这样获得格式化的字符串:

DateTime date = new DateTime(2011, 2, 19);
string formatted = date.ToString("dd/M/yyyy");

#3


12  

As everyone else said, but remember CultureInfo.InvariantCulture!

正如其他人所说,但记得CultureInfo.InvariantCulture!

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)

OR escape the '/'.

或者逃避'/'。

#4


8  

DateTime.ToString("dd/MM/yyyy") may give the date in dd-MM-yyyy format. This depends on your short date format. If short date format is not as per format, we have to replace character '-' with '/' as below:

DateTime.ToString(“dd / MM / yyyy”)可以以dd-MM-yyyy格式给出日期。这取决于您的短日期格式。如果短日期格式不符合格式,我们必须将字符' - '替换为'/',如下所示:

 date = DateTime.Now.ToString("dd/MM/yyyy").Replace('-','/');

#5


5  

It's simple--tostring() accepts a parameter with this format...

这很简单 - tostring()接受这种格式的参数......

DateTime.ToString("dd/MM/yyyy");

#6


5  

You have to pass the CultureInfo to get the result with slash(/)

你必须通过CultureInfo来获得斜杠(/)的结果

DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)

#7


3  

You can use the ToString() method, if you want a string representation of your date, with the correct formatting.

如果希望使用正确的格式设置日期的字符串表示,则可以使用ToString()方法。

Like:

喜欢:

DateTime date = new DateTime(2011, 02, 19);
string strDate = date.ToString("dd/MM/yyyy");

#8


3  

Here is a method, that takes datetime(format:01-01-2012 12:00:00) and returns string(format: 01-01-2012)

这是一个方法,它采用datetime(格式:01-01-2012 12:00:00)并返回字符串(格式:01-01-2012)

public static string GetDateFromDateTime(DateTime datevalue){
    return datevalue.ToShortDateString(); 
}

#9


1  

If you want the string use -

如果你想要字符串使用 -

DateTime.ToString("dd/MM/yyyy")

#10


1  

On my login form I am showing the current time on a label.

在我的登录表单上,我在标签上显示当前时间。

    public FrmLogin()
    {
        InitializeComponent();
        lblTime.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    }

    private void tmrTime_Tick(object sender, EventArgs e)
    {
        lblHora.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    }

#11


-1  

This works for me:

这对我有用:

string dateTimeString = "21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30";
dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);

string inputFormat = "dd-MM-yyyy HH:mm:ss";
string outputFormat = "yyyy-MM-dd HH:mm:ss";
var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
string output = dateTime.ToString(outputFormat);

Console.WriteLine(output);

#12


-5  

this is you need and all people

这是你需要和所有人

   string date  = textBox1.Text;

        DateTime date2 = Convert.ToDateTime(date);
        var date3 = date2.Date;
        var D = date3.Day;
      var M =  date3.Month;         
      var y = date3.Year;
      string monthStr = M.ToString("00");
      string date4 = D.ToString() + "/" + monthStr.ToString() + "/" + y.ToString();


      textBox1.Text = date4;

#1


74  

DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

#2


18  

First of all, you don't convert a DateTime object to some format, you display it in some format.

首先,您不将DateTime对象转换为某种格式,而是以某种格式显示它。

Given an instance of a DateTime object, you can get a formatted string in that way like this:

给定一个DateTime对象的实例,您可以像这样获得格式化的字符串:

DateTime date = new DateTime(2011, 2, 19);
string formatted = date.ToString("dd/M/yyyy");

#3


12  

As everyone else said, but remember CultureInfo.InvariantCulture!

正如其他人所说,但记得CultureInfo.InvariantCulture!

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)

OR escape the '/'.

或者逃避'/'。

#4


8  

DateTime.ToString("dd/MM/yyyy") may give the date in dd-MM-yyyy format. This depends on your short date format. If short date format is not as per format, we have to replace character '-' with '/' as below:

DateTime.ToString(“dd / MM / yyyy”)可以以dd-MM-yyyy格式给出日期。这取决于您的短日期格式。如果短日期格式不符合格式,我们必须将字符' - '替换为'/',如下所示:

 date = DateTime.Now.ToString("dd/MM/yyyy").Replace('-','/');

#5


5  

It's simple--tostring() accepts a parameter with this format...

这很简单 - tostring()接受这种格式的参数......

DateTime.ToString("dd/MM/yyyy");

#6


5  

You have to pass the CultureInfo to get the result with slash(/)

你必须通过CultureInfo来获得斜杠(/)的结果

DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)

#7


3  

You can use the ToString() method, if you want a string representation of your date, with the correct formatting.

如果希望使用正确的格式设置日期的字符串表示,则可以使用ToString()方法。

Like:

喜欢:

DateTime date = new DateTime(2011, 02, 19);
string strDate = date.ToString("dd/MM/yyyy");

#8


3  

Here is a method, that takes datetime(format:01-01-2012 12:00:00) and returns string(format: 01-01-2012)

这是一个方法,它采用datetime(格式:01-01-2012 12:00:00)并返回字符串(格式:01-01-2012)

public static string GetDateFromDateTime(DateTime datevalue){
    return datevalue.ToShortDateString(); 
}

#9


1  

If you want the string use -

如果你想要字符串使用 -

DateTime.ToString("dd/MM/yyyy")

#10


1  

On my login form I am showing the current time on a label.

在我的登录表单上,我在标签上显示当前时间。

    public FrmLogin()
    {
        InitializeComponent();
        lblTime.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    }

    private void tmrTime_Tick(object sender, EventArgs e)
    {
        lblHora.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
    }

#11


-1  

This works for me:

这对我有用:

string dateTimeString = "21‎-‎10‎-‎2014‎ ‎15‎:‎40‎:‎30";
dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);

string inputFormat = "dd-MM-yyyy HH:mm:ss";
string outputFormat = "yyyy-MM-dd HH:mm:ss";
var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
string output = dateTime.ToString(outputFormat);

Console.WriteLine(output);

#12


-5  

this is you need and all people

这是你需要和所有人

   string date  = textBox1.Text;

        DateTime date2 = Convert.ToDateTime(date);
        var date3 = date2.Date;
        var D = date3.Day;
      var M =  date3.Month;         
      var y = date3.Year;
      string monthStr = M.ToString("00");
      string date4 = D.ToString() + "/" + monthStr.ToString() + "/" + y.ToString();


      textBox1.Text = date4;