Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串转换为12小时格式的日期时间格式_C#_Asp.net_Datetime - Fatal编程技术网

C# 将字符串转换为12小时格式的日期时间格式

C# 将字符串转换为12小时格式的日期时间格式,c#,asp.net,datetime,C#,Asp.net,Datetime,我在SQL server数据库中有一个字段类型DateTime。 我正在开发一个Web服务来获取日期和时间 日期以这种格式存储在数据库中,即2013年4月14日10:10:01下午 现在,我有一个webmethod,如下所示: public string GetdataJson(string lastdate) { DateTime myDateTime = DateTime.Parse(lastdate); string g

我在SQL server数据库中有一个字段类型DateTime。 我正在开发一个Web服务来获取日期和时间

日期以这种格式存储在数据库中,即2013年4月14日10:10:01下午

现在,我有一个webmethod,如下所示:

public string GetdataJson(string lastdate)
        {

            DateTime myDateTime = DateTime.Parse(lastdate);


            string getvalue = "select * from tblfameface where last_updated >='" + myDateTime  + "'";

            con1 = new SqlConnection(conString1);
            con1.Open();
            SqlCommand cmd = new SqlCommand(getvalue, con1);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt1 = new DataTable();
            da.Fill(dt1);

            string jsonString = JsonConvert.SerializeObject(dt1);
            String finalString = "{\"Records\":";
            finalString += jsonString;
            finalString += "}";
            return finalString;

        }
但这段代码给了我一个错误,字符串未被识别为有效的日期时间。 如何将字符串转换为日期时间格式,如4/14/2013 10:10:01 PM??
救命啊

在格式字符串中,将
tt
用于PM/AM部分。代码如下:

 DateTime dateTime = 
    DateTime.ParseExact("4/14/2013 10:10:01 PM", 
                        "M/dd/yyyy hh:mm:ss tt",
                        System.Globalization.CultureInfo.InvariantCulture);
这也应该起作用:

DateTime result = DateTime.Parse("04/14/2013 10:10:01 PM");
您确定得到的字符串正确吗?

请使用此代码

DateTime.ParseExact(yourString, "MM/dd/yyyy hh:mm:ss tt");

你想要12小时的时钟用小hh,你想要24小时的时钟用大写hh

DateTime formatted =Covert.ToDateTime( lastdate.ToString("dd/MM/yyyy hh:mm:ss.fff",
                                      CultureInfo.InvariantCulture));
如果要使用12小时时钟,请在格式字符串中使用tt生成AM/PM指示符。

尝试以下操作:

string getvalue = "select * from tblfameface where
     last_updated >= Convert(DateTime, '" + myDateTime  + "')";
或者,这取决于上次更新的列上的SQL数据类型

string getvalue = "select * from tblfameface where
     last_updated >= Convert(DateTime2(7), '" + myDateTime  + "')";
将查询行替换为

string getvalue = "select * from tblfameface where CAST(last_updated AS 
DATETIME)>='" + myDateTime.ToString("dd/MM/yyyy hh:mm:ss tt")  + "'";

您发送给此webmethod的输入是什么?我正在通过2013年4月14日10:10:01 pm刚刚确认:您在哪一行收到异常?我认为这可能是参数
lastdate
中的意外输入,您有一个使用24小时时钟的区域性变体”,因为DateTime.TryParse(字符串, DateTime)方法尝试使用当前区域性的格式规则分析日期和时间的字符串表示形式,尝试跨不同区域性分析特定字符串可能会失败或返回不同的结果。如果将跨不同地区分析特定的日期和时间格式,请使用DateTime.TryParse(字符串, 提供程序, 日期时间样式, DateTime)方法或TryParseExact的重载之一..“@Hossein Narimani Rad您的代码出现错误,ParseExact的重载方法不接受2个参数。这不在我的情况下,现在该怎么办?上面的代码非常有效..甚至Convert.ToDateTime()也有效..请使用输入检查..好,告诉我用一个12小时格式的好例子,告诉她你正在把一个字符串转换成12小时格式是一个好例子?使用类似于
2/2/13 23:40:45 PM
的格式,并将其转换为12小时格式!继续@Khushbu,你发现有什么有用的吗?如果是,请让我知道!