locked
How to convert date? RRS feed

  • Question

  • User1782190181 posted

    Hello! I would like to convert my date which i got from events from Fullcalendar. Date looks like "2015-01-28T11:30:00.000Z". I wrote Json method which get my reqest.

    public JsonResult GetReservation(int doctor, string date)
            {
                RegisterVisitModel model = new RegisterVisitModel();
                model.doctor = DoctorRepository.GetDoctorByID(doctor);
                model.dateVisit = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss,fff", null); 
    
                return Json(new
                {
                    redirectUrl = Url.Action("WypelnijFormularz", "Dentist",model),
                    isRedirect = true
                });
            }
    here its error An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

    How can i parse string date to Datetime type? or how can i get string date without letter T and Z??

    Greetingss!

    Tuesday, January 27, 2015 5:38 PM

Answers

  • User-434868552 posted

    @Rejentt

    String rejenttDateTime = "2015-01-28T11:30:00.000Z";
    Console.WriteLine (rejenttDateTime.Substring(0,10) );
    Console.WriteLine (rejenttDateTime.Substring(11, 12));
    DateTime parsedDateTIme = DateTime.Parse(rejenttDateTime.Substring(0,10) + " " + rejenttDateTime.Substring(11, 12));
    Console.WriteLine (parsedDateTIme);
    2015-01-28
    11:30:00.000
    2015-01-28 11:30:00



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2015 5:53 PM
  • User-1716253493 posted

    Try this

    model.dateVisit = DateTime.ParseExact(date, "o", null); 
    model.dateVisit = DateTime.ParseExact(date, "u", null); 

    or

    model.dateVisit = DateTime.ParseExact(date, "yyyy-MM-ddTHH:mm:ss.000Z", null); 

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2015 7:19 PM

All replies

  • User-434868552 posted

    @Rejentt

    String rejenttDateTime = "2015-01-28T11:30:00.000Z";
    Console.WriteLine (rejenttDateTime.Substring(0,10) );
    Console.WriteLine (rejenttDateTime.Substring(11, 12));
    DateTime parsedDateTIme = DateTime.Parse(rejenttDateTime.Substring(0,10) + " " + rejenttDateTime.Substring(11, 12));
    Console.WriteLine (parsedDateTIme);
    2015-01-28
    11:30:00.000
    2015-01-28 11:30:00



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2015 5:53 PM
  • User-1716253493 posted

    Try this

    model.dateVisit = DateTime.ParseExact(date, "o", null); 
    model.dateVisit = DateTime.ParseExact(date, "u", null); 

    or

    model.dateVisit = DateTime.ParseExact(date, "yyyy-MM-ddTHH:mm:ss.000Z", null); 

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2015 7:19 PM