none
sharepoint 2010 - how set DataTime field format

    Question

  • Hi guys,

    I have a e custom Field with Type="DateTime" and Format="DateTime". In the behind code, when I add an item into the list, I need to set the value in the format dd/MM/yyyy HH:mm but it doesn't works. The problem is that if I prove with DateTime.Today.ToString("d") + DateTime.Now.ToString("HH:mm") it shows MM/dd/yyyy HH:mm format and if I prove with DateTime.ParseExact(DateTime.Today.ToString("d"), "dd/MM/yyyy", new CultureInfo("it-IT")) + " " + DateTime.Now.ToString("HH:mm") it throws the Exception String was recognized as a valid DateTime. I proved also writing separate Now.Day+ "/" + Now.Month+"/"+Now.Years but the result doesn't change, it shows MM/dd/yyyy HH:mm format.

    Could anyone help me please???

    thanks

    Wednesday, July 11, 2012 10:56 AM

Answers

  • Whenever you want to save date time in sharepoint lists it takes the format MM/dd/yyyy,Its the default format for datetime in sharepoint.

    Thanks, Sudhanshu Shekhar(MCTS)

    It is not true... I solved!!!

    I saw that if I had printed DateTime.ParseExact(DateTime.Today.ToString("d"), "dd/MM/yyyy" and it shows dd/MM/yyyy 00:00:00 and so I solved the problem by replacing 00:00:00 with DateTime.Now.ToString("HH:mm") !!!

    string date=DateTime.ParseExact(DateTime.Today.ToString("d"), "dd/MM/yyyy", new CultureInfo("it-IT")).ToString();
    
    item["field"] = date.Substring(0, date.Length - 8) + DateTime.Now.ToString("HH:mm");


    thanks!!! :D


    • Marked as answer by bollett Wednesday, July 11, 2012 1:51 PM
    • Unmarked as answer by bollett Wednesday, July 11, 2012 1:51 PM
    • Edited by bollett Wednesday, July 11, 2012 1:52 PM
    • Marked as answer by bollett Wednesday, July 11, 2012 1:52 PM
    Wednesday, July 11, 2012 1:51 PM

All replies

  • Whenever you want to save date time in sharepoint lists it takes the format MM/dd/yyyy,Its the default format for datetime in sharepoint.

    Thanks, Sudhanshu Shekhar(MCTS)

    Wednesday, July 11, 2012 11:31 AM
  • Whenever you want to save date time in sharepoint lists it takes the format MM/dd/yyyy,Its the default format for datetime in sharepoint.

    Thanks, Sudhanshu Shekhar(MCTS)

    It is not true... I solved!!!

    I saw that if I had printed DateTime.ParseExact(DateTime.Today.ToString("d"), "dd/MM/yyyy" and it shows dd/MM/yyyy 00:00:00 and so I solved the problem by replacing 00:00:00 with DateTime.Now.ToString("HH:mm") !!!

    string date=DateTime.ParseExact(DateTime.Today.ToString("d"), "dd/MM/yyyy", new CultureInfo("it-IT")).ToString();
    
    item["field"] = date.Substring(0, date.Length - 8) + DateTime.Now.ToString("HH:mm");


    thanks!!! :D


    • Marked as answer by bollett Wednesday, July 11, 2012 1:51 PM
    • Unmarked as answer by bollett Wednesday, July 11, 2012 1:51 PM
    • Edited by bollett Wednesday, July 11, 2012 1:52 PM
    • Marked as answer by bollett Wednesday, July 11, 2012 1:52 PM
    Wednesday, July 11, 2012 1:51 PM