转换日期时间格式!!请帮忙 [英] convert date time format !! please help

查看:63
本文介绍了转换日期时间格式!!请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string Fulldate = Request["msgDate"]; //"3/17/2014 10:32:35 AM"
            string SDate = string.Format("{0:yyyy-mm-dd}", Fulldate); // its not converting to 2014-03-17

推荐答案

1。首先,使用tryparse检查日期字符串是否有效;

2.指定所需的日期格式;

3.然后可以进行转换。

1. First, check that the date string is a valid one using tryparse;
2. Specify the desired date format;
3. then can do the conversion.
using System;

public class Program
{
    public static void Main()
    {
          // change 2/22/2014 5:08:46 PM to 2014-02-22.
          string dateString = "2/22/2014 5:08:46 PM";

          Console.WriteLine("The original string is {0}", dateString);

          DateTime dt;

          if (DateTime.TryParse(dateString, out dt))
          {
               string format = "yyyy-MM-dd";
               Console.WriteLine(dt.ToString(format));
          }
          else
          {
              Console.WriteLine("Not a valid datetime");
          }
    }
}


您将字符串传递给 string.Format 方法。

FullDate 变量必须是 DateTime 对象。

It's beacuase you're passing string to string.Format method.
FullDate variable must be DateTime object.
DateTime Fulldate = Request["msgDate"]; //"3/17/2014 10:32:35 AM"
string SDate = string.Format("{0:yyyy-mm-dd}", Fulldate);





希望它帮助您。



Hope it help you.


使用此链接 http://www.csharp-examples.net/string-format-datetime/</a> [ ^ ]

肯定会对你有帮助。
Use this link http://www.csharp-examples.net/string-format-datetime/</a>[^]
It will definitely help you.


这篇关于转换日期时间格式!!请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆