You must Sign In to post a response.
  • Category: ASP.Net MVC

    String was not recognized as a valid DateTime in ASP.Net MVC

    Hi,

    I am creating one web application in ASP.Net MVC. I got error while running the application.

    Error Message is String was not recognized as a valid DateTime. I am giving my code here.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVCApp.Models;
    using System.ComponentModel;

    namespace MVCApp.Controllers {
    public class HomeController : Controller {

    public ActionResult Index() {

    Person myPerson = new Person {
    PersonId = 1,
    FirstName = "RaJ",
    LastName = "Kumar",
    BirthDate = DateTime.Parse("25/02/1989"),
    HomeAddress = new Address {
    Line1 = "123 North Street",
    Line2 = "guindy",
    City = "Chennai",
    Country = "India",
    PostalCode = "600 032"
    },
    IsApproved = true,
    Role = Role.User
    };
    return View(myPerson);
    }

    public ActionResult SimpleModel() {
    return View(new SimpleModel {
    Name = "Raj Kumar",
    Status = Role.Guest
    });
    }

    }
    }


    Thanks and Regards
    S.Suresh
  • #747689
    Before type casting the string value to a data time variable, please convert the value.



    BirthDate = Convert.ToDateTime("25/02/1989"),


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #747693
    Hi,

    As per error description there is an issue while convert datetime into string format.

    Refer below link how to convert datetime into string as well as string into datetime.

    http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For

    Hope this will help you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #747707

    you have passed date in wrong format to the DateTime.Parse method. The format of the date should be MM/DD/YYYY. Please correct your statement to the one as I have shown below in the MM/DD/YYYY format.

    BirthDate = DateTime.Parse("25/02/1989")

    should be written as

    BirthDate = DateTime.Parse("02/25/1989")

    This should work.


    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #747738
    Hi,
    The format needs to be passed is as below.

    DateTime Bday = DateTime.Parse("26/01/1995");

    All is well.
    Thanks,
    Dharma
    Editor,Mentor,MVM
    Try and fail but don't fail and try

  • #747789
    Hi dude,

    Thanks for all giving the valuable answer. I solved the problem.

    Thanks and regards
    S.suresh

    "IF YOU WANT TO SHINE LIKE A SUN FIRST YOU HAVE BURN LIKE IT".

  • #749985
    Hello
    You have to use Culture also so that it will convert accordingly

    DateTime dt= Convert.ToDateTime("26/01/1995", CultureInfo.CurrentCulture);

    or
    DateTime dt= Convert.ToDateTime("26/01/1995", CultureInfo.CultureInfo.InvariantCulture);


    Hope it will help you

    Thanks


  • Sign In to post your comments