DateTime and ParseExact not working properly

I am using AJAX and sending certain data:

var eventtype = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("EVENT_TYPE_ID").GetFieldDataFieldId()%>); var begindate = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("BeginDate").GetFieldDataFieldId()%>); var enddate = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("EndDate").GetFieldDataFieldId()%>); $.ajax({ type: "POST", url: "Data.aspx/CheckInsertRecord", data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," + "EndDate:'" + enddate+"' }", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg.d); } });

In the code behind:

CultureInfo provider = CultureInfo.InvariantCulture; DateTime bd = DateTime.ParseExact(BeginDate,"MM/DD/YYYY", provider); DateTime ed = DateTime.ParseExact(EndDate, "MM/DD/YYYY", provider);

However when I run it i get an error saying

"String was not recognized as a valid Datetime"

Can u please help me in getting rid of this.

Or is there any other way to convert string to a DateTime?

-------------Problems Reply------------

In .NET, a correct custom date/time format string for a date string like "01/01/2011" is "MM/dd/yyyy"

Try

DateTime bd = DateTime.ParseExact(BeginDate,"MM/dd/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "MM/dd/yyyy", provider);

or

DateTime bd = DateTime.ParseExact(BeginDate,"M/d/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "M/d/yyyy", provider);

Category:c# Views:1 Time:2011-06-13
Tags: asp.net jquery

Related post

  • DateTime And ParseExact Problems 2010-06-08

    Hi There I wish to convert my string in format dd/mm/yyyy hh:mm:ss to a DateTime of the same format. Dim ukCulture As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-GB") Dim myDateTime As DateTime myDateTime = DateTime.Pa

  • Convert Between Datetime Formats Using Perl 2010-02-12

    We're currently using a 3rd party API that provides datetime in the following format: Sat Mar 06 09:00:00 ICST 2010 Fri Feb 19 19:30:00 JDT 2010 Fri Feb 19 19:30:00 PST 2010 However, we want to store these datetime objects in MySQL in a standard date

  • C# Open Xml SDK 2.0 Spreadsheet Setting Cell DateTime Format 2010-07-22

    I'm having trouble finding this one. my situation: SDK 2.0 no template spreadsheet C# 4.0 in VS2010 my problem: Certain data in the excel files I want to build exists in DateTime format. As I don't wan't to use just strings (stringed datetimes can't

  • jqGrid colModel DateTime sort 2010-12-06

    How can i get the the JqGrid column to sort as per date time, here is what i tried. { "name": "PACT_DATE", "index": "PACT_DATE", "width": 55, "jsonmap":"PACT_DATE", "sorttype": "date", "datefmt": "Y-m-d H:i:s" } I am still not getting my columns data

  • Parse a full datetime string to DateTime type 2011-06-08

    I need to Parse this string "Wed Jun 01 2011 00:00:00 GMT+0300 (GTB Daylight Time)" into a DateTime! can this be done in .NET Framework? [EDIT] I think the problem is how to express this (GTB Daylight Time)... Thank you. --------------Solutions------

  • How to Convert DateTime? 2011-08-27

    I am currently pulling start times from an XML file and using a date checking method to filter and then display in a Listbox.Itemsource. Currently the start times are in "yyyyMMddHHmmss zzz" format in my XML file and my DateChecking method is also th

  • CRM 2011: Set datetime field from string with javascript 2011-09-01

    I'm trying to create a new record where one of the fields is a DataTime field. I always get the "The date-time format for Thu Sep 01 19:25:20 2011 is invalid, or value is outside the supported range." error. I've tried multiple format options. With D

  • How do I convert a TimeSpan to a formatted string? 2009-05-08

    Possible Duplicate: How can I String.Format a TimeSpan object with a custom format in .NET? Exact Duplicate Timespan formatting First question here: I have two DateTime vars, beginTime and endTime. I have gotten the difference of them by doing the fo

  • Chained aggregate call across association in DataMapper (ruby) 2009-10-24

    I am working on a simple budget app using Sinatra and DataMapper in Ruby. I want to get the sum of all transactions across all income accounts within the last 30 days. Something like Account.income_accounts.account_entries.sum(:amount, :transaction_d

  • Get day from the given date 2010-02-08

    Using VB.Net 2005 I want to get a day for the date. My Datepicker format is Custom Format (dd/MM/yyyy) Code. Dim dt As New DateTime() dt = Format(textdate.Text, "dd/MM/yyyy") textday.Text = dt.DayOfWeek.ToString When i try to run the above code, it w

  • C# - Calculate min date / max date from a List 2010-02-25

    I have a List which contains Dates : List<string> StringDates; [0]: "04.03.2010" [1]: "09.03.2010" [2]: "11.03.2010" [3]: "12.03.2010" [4]: "16.03.2010" [5]: "18.03.2010" [6]: "19.03.2010" [7]: "23.03.2010" [8]: "25.03.2010" [9]: "26.03.2010" U

  • rails group_by relative date ranges 2011-07-12

    I have a client who wants to see a report of her employees group by the date they took their last position (date_in_position). She wants them grouped by Less than 1 year, 1-3 years, 3-5 years, and over 5 years. I made a little method to return a stri

  • CRM 2011 - Compare years in LINQ Query - Gives an error : Invalid 'where' condition. An entity member is invoking an invalid property or method 2012-01-03

    I've tried several methods to compare years in a date, but each time I'm getting the "Invalid 'where' condition. An entity member is invoking an invalid property or method."-error! The methods I've used : var openInvoices = orgContext.CreateQuery<

  • Why can't DateTime.ParseExact() parse "9/1/2009" using "M/d/yyyy" 2009-09-02

    I have a string that looks like this: "9/1/2009". I want to convert it to a DateTime object (using C#). This works: DateTime.Parse("9/1/2009", new CultureInfo("en-US")); But I don't understand why this doesn't work: DateTime.ParseExact("9/1/2009", "M

  • Parsing "HH:mm:ss" and "H:m:s" times using DateTime.ParseExact() 2009-11-09

    I want to be able to parse the following times using ParseExact() function: 01:02:03 1:02:03 1:2:03 1:02:3 01:2:3 1:2:3 Is it possible, or do I need to do my own conversions so it work? So far I can parse only all with leading 0 or all without leadin

  • DateTime.ParseExact string format exception 2010-01-04

    I am trying to convert a string into datetime with the following C# code, DateTime dTo = DateTime.ParseExact(dateTo, "mm/dd/yyyy", CultureInfo.InvariantCulture); eachtime I pass dateTo as 1/1/2010 it fails, instead it needs the string to be 01/01/201

  • String was not recognized as a valid DateTime ParseExact 2010-02-02

    I am trying to parse a date in c# and have the following line of code DateTime.ParseExact(DateSelected, "ddd MMM dd HH:mm:ss zzz yyyy", Culture) when I debug DateSelected is coming in as "Tue Feb 16 12:36:41 CST 2010" but I get an expection saying "S

  • Why can't DateTime.ParseExact() parse the AM/PM in "4/4/2010 4:20:00 PM" using "M'/'d'/'yyyy H':'mm':'ss' 'tt" 2010-04-08

    I'm using c#, and if I do DateTime.ParseExact("4/4/2010 4:20:00 PM", "M'/'d'/'yyyy H':'mm':'ss' 'tt", null) The return value is always 4:20 AM -- what am I doing wrong with using tt? Thanks! --------------Solutions------------- Make the hour format (

  • DateTime.ParseExact fails depending on the parsed string's length 2010-04-14

    This is very weird. I have the following code: Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact("2000", "yyyy", CultureInfo.InvariantCulture)); Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact("20000705", "yyyy", CultureInfo

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.169 (s). 11 q(s)