将dateTime转换为所需的格式 [英] Converting dateTime to desired format

查看:75
本文介绍了将dateTime转换为所需的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下方法,通过存储过程将数据插入数据库。它以以下格式存储日期时间:YY / MM / DD HH:MM:SS:00但我想从中删除:SS部分并希望将其存储为:YY / MM / DD HH:MM。我尝试了很多东西,但是帮助我。请帮助



代码:



I have written the below method which inserts data into the database via a store procedure. It stores datetime in the format : YY/MM/DD HH:MM:SS:00 but i want to remove :SS part from it and want to store it like this: YY/MM/DD HH:MM. I tried many things, but dint help. Please help

CODE:

public bool InsertCoordinates(string param)
    {

        String[] parts = param.Split(',');
        sqlCom.CommandText = "InsertCoordinates";
        sqlCom.CommandType = CommandType.StoredProcedure;
        bool result ;

        sqlCom.Parameters.Add("@AddedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[0].ToString());
        sqlCom.Parameters.Add("@RecordedDateTime", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[2].ToString());


        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
            result = Convert.ToBoolean(sqlParam.Value);
            return result;
        }







怎么样?




how ?

推荐答案

永远不要以字符串格式将日期信息存储在数据库中 - 始终是用户SQL DATETIME字段,并直接传递C#DateTime值而不转换它。



String存储日期是一个可以使用的PITA:它们很难比较,很难用数学计算。

DateTime值可以更容易地使用,并且在检索显示时完全按照您的需要进行格式化。
Never store Date information in a database in string format - always user SQL DATETIME fields, and pass the C# DateTime value directly without converting it.

String stored dates are a PITA to work with: they are difficult to compare, difficult to do math with.
DateTime values can be used much more easily, and formatted exactly as you need when they are retrieved for display.


如果您使用storeProcedure保存数据库中的数据,那么为什么不能使用这个




if you are using storeProcedure for saving data in your database then why can't you use this


select  CAST('07-25-08 12:35:29.123' AS smalldatetime) AS
        'smalldatetime'


这篇关于将dateTime转换为所需的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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