locked
Database Time is coming as 00:00:00 RRS feed

  • Question

  • User219039814 posted

    My database is  2018-07-03 12:30:00.000

    But when i convert to JSON..

    it comes as 2018-07-03 00:00:00.000

    i did all the conversion required in my web method.

    Can anyone tell me what's going on

    Friday, June 29, 2018 6:32 AM

Answers

  • User-1171043462 posted

    If Date is coming correct from Database i.e. with Time then JavaScriptSerializer will also show correct values.

    I think problem is in your Stored Proc or how you bring.

    See this example, it works fine

    public class MyData
        {
            public string Date { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List<object> myList = new List<object>();
            myList.Add(new MyData { Date = "2018-07-03 12:30:00.000" });
            string json = (new JavaScriptSerializer()).Serialize(myList);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 30, 2018 1:54 PM
  • User-330142929 posted

    Hi Vijaylakshmi,

    According to your codes and description, I do not quite understand you.

    vijaylakshmi

    My database is  2018-07-03 12:30:00.000

    But when i convert to JSON..

    it comes as 2018-07-03 00:00:00.000

    i did all the conversion required in my web method.

    In my opinion, here your question involves three Datetime types, SqlServer,C# and Javascript. As far as I know, When we retrieve the database Datatime type via ado.net and then convert it to a C# DataTime type, There is no loss of precision here. If you are looking at the Json string through the front end, I suggest that you check how javascript format Datetime data. So I don't quite understand how you check the result of the conversion. If you are looking at Datatime result on the C# back end, I have made an example here,wish it is useful to you.

    It looks like no problem here. I use two ways to store the datetime format.

    Code behind.

      protected void Page_load(object sender,EventArgs e)
            {   
            }
            [WebMethod]
            public static string GetData()
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDataStore"].ConnectionString);
                SqlCommand command = new SqlCommand();
                command.CommandText = "select Datetime from DatetimeRecords where id=1";
                command.CommandType = System.Data.CommandType.Text;
                command.Connection = connection;
                connection.Open();
                var result = Convert.ToDateTime(command.ExecuteScalar());
                connection.Close();
                //List<object> list = new List<object>();
                Data data = new Data() { dateTime = result };
                Data1 data1 = new Data1() { datatime = result.ToString() };
                //list.Add(data);
                string json = new JavaScriptSerializer().Serialize(data);
                string json1 = new JavaScriptSerializer().Serialize(data1);
                return json;
            }
     
        }
       public class Data
        {
            public DateTime dateTime { get; set; }
        }
        public class Data1
        {
            public string datatime { get; set; }
     
        }

    When we use string type to store it. We could directly see the json1 result.

    when we use Datetime Type to store the Datatime. We could check json result by Visula Studio advanced debugging system.

    Please post more details about how you check the json string result. It is easily to give you an effective reply if you could post more details.

    Feel free to let me know if you have any question.

    Best Regards.

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 8:57 AM

All replies

  • User1992938117 posted

    From Database it comes to c# code, what is the value there? how you created JSON data?

    Can you share the code...

    Friday, June 29, 2018 6:45 AM
  • User-1716253493 posted

    Seem like you have convert datetime value into date datatype

    Friday, June 29, 2018 6:55 AM
  • User-1171043462 posted

    Share your code

    Friday, June 29, 2018 8:20 AM
  • User219039814 posted

    Connection con = new Connection();
    SqlConnection conn = new SqlConnection();


    conn = con.getConnection();
    conn.Open();
    String query = "SELECT * FROM booking";

    SqlCommand cmd = new SqlCommand(query, conn);

    SqlDataReader reader = cmd.ExecuteReader();

    string myJsonString = "";
    List<object> myList = new List<object>();


    if (reader.HasRows)
    {
    var an = reader.GetOrdinal("AuditoriumName");

    var bstartdate = reader.GetOrdinal("BookingStartDate");
    var benddate = reader.GetOrdinal("BookingEndDate");
    var bfromtime = reader.GetOrdinal("BookingFromTime");
    var btotime = reader.GetOrdinal("BookingToTime");


    while (reader.Read())
    {

    var audiname = reader.GetValue(an).ToString();

    var bstartdate1 = Convert.ToDateTime(reader.GetValue(bstartdate));
    var benddate1 = Convert.ToDateTime(reader.GetValue(benddate));


    DateTime RealStartDate = Convert.ToDateTime(bstartdate1);
    DateTime RealEndDate = Convert.ToDateTime(bstartdate1);

    String SendStartDate = RealStartDate.ToString("s");
    String SendEndDate = RealEndDate.ToString("s");

    var bfromtime1 = reader.GetValue(bfromtime).ToString();
    var btotime1 = reader.GetValue(btotime).ToString();

    timetable t_table = new timetable(audiname, SendStartDate, SendEndDate,bfromtime1,btotime1);

    myList.Add(t_table);

    }


    myJsonString = (new JavaScriptSerializer()).Serialize(myList);


    conn.Close();
    }

    return myJsonString;

    Friday, June 29, 2018 9:15 AM
  • User1992938117 posted

    Somehow JavaScriptSerialization shows nothing to me hence I used Newtonsoft JSON and I can see proper data.

    using Newtonsoft.Json;
    var json = JsonConvert.SerializeObject(myList);

    Result

    [{"audiname":"Aud01","sendStartDate":"2018-07-03T12:30:00","sendEndDate":"2018-07-03T12:30:00","bfromtime1":"7/3/2018 12:30:00 PM","btotime1":"7/3/2018 12:30:00 PM"},{"audiname":"Aud02","sendStartDate":"2018-07-03T12:30:00","sendEndDate":"2018-07-03T12:30:00","bfromtime1":"7/3/2018 12:30:00 PM","btotime1":"7/3/2018 12:30:00 PM"}]

    Can you share 

    Friday, June 29, 2018 9:48 AM
  • User219039814 posted
    Do i have to download newtonsoft.json.. library??
    Friday, June 29, 2018 1:59 PM
  • User1992938117 posted

    Yes,

    you need to use Nuget Package in Visual Studio project, also can be downloaded separately.

    https://www.nuget.org/packages/newtonsoft.json/ 

    Friday, June 29, 2018 2:03 PM
  • User219039814 posted

    Thank you sir, i will try it out.

    Saturday, June 30, 2018 8:59 AM
  • User-1171043462 posted

    If Date is coming correct from Database i.e. with Time then JavaScriptSerializer will also show correct values.

    I think problem is in your Stored Proc or how you bring.

    See this example, it works fine

    public class MyData
        {
            public string Date { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List<object> myList = new List<object>();
            myList.Add(new MyData { Date = "2018-07-03 12:30:00.000" });
            string json = (new JavaScriptSerializer()).Serialize(myList);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 30, 2018 1:54 PM
  • User-330142929 posted

    Hi Vijaylakshmi,

    According to your codes and description, I do not quite understand you.

    vijaylakshmi

    My database is  2018-07-03 12:30:00.000

    But when i convert to JSON..

    it comes as 2018-07-03 00:00:00.000

    i did all the conversion required in my web method.

    In my opinion, here your question involves three Datetime types, SqlServer,C# and Javascript. As far as I know, When we retrieve the database Datatime type via ado.net and then convert it to a C# DataTime type, There is no loss of precision here. If you are looking at the Json string through the front end, I suggest that you check how javascript format Datetime data. So I don't quite understand how you check the result of the conversion. If you are looking at Datatime result on the C# back end, I have made an example here,wish it is useful to you.

    It looks like no problem here. I use two ways to store the datetime format.

    Code behind.

      protected void Page_load(object sender,EventArgs e)
            {   
            }
            [WebMethod]
            public static string GetData()
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDataStore"].ConnectionString);
                SqlCommand command = new SqlCommand();
                command.CommandText = "select Datetime from DatetimeRecords where id=1";
                command.CommandType = System.Data.CommandType.Text;
                command.Connection = connection;
                connection.Open();
                var result = Convert.ToDateTime(command.ExecuteScalar());
                connection.Close();
                //List<object> list = new List<object>();
                Data data = new Data() { dateTime = result };
                Data1 data1 = new Data1() { datatime = result.ToString() };
                //list.Add(data);
                string json = new JavaScriptSerializer().Serialize(data);
                string json1 = new JavaScriptSerializer().Serialize(data1);
                return json;
            }
     
        }
       public class Data
        {
            public DateTime dateTime { get; set; }
        }
        public class Data1
        {
            public string datatime { get; set; }
     
        }

    When we use string type to store it. We could directly see the json1 result.

    when we use Datetime Type to store the Datatime. We could check json result by Visula Studio advanced debugging system.

    Please post more details about how you check the json string result. It is easily to give you an effective reply if you could post more details.

    Feel free to let me know if you have any question.

    Best Regards.

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 8:57 AM