System.DateTime.Parse("Thu, 05 May 2005 14:50:52 EDT") Fails

  • Thread starter Thread starter Michael S. Scherotter
  • Start date Start date
M

Michael S. Scherotter

How do I use the .Net 1.1 System.DateTime so that it will parse the string

"Thu, 05 May 2005 14:50:52 EDT"

Currently System.DateTime.Parse("Thu, 05 May 2005 14:50:52 EDT") Fails with

a FormatException:
The string was not recognized as a valid DateTime. There is a unknown word
starting at index 26.

Thanks,
Michael
 
Hi Michael,

DateTime isn't able to parse time zone abbreviations like that. For instance EDT could be located in either Australia or North America, and DateTime would have no way of knowing which.

You need to change the time zone abbreviation into a format using +HH:mm
In your case

"Thu, 05 May 2005 14:50:52+07:00"

Where +07:00 should be the difference between EDT and your current time zone.
 
I just started getting this message on 5/4/05! I'm not parsing and before
this, my program worked just fine. Here's a posting I put on another site
in desparation:


Greetings,

I don't know how long ago this conversation took place, but am desperate
for help with the same problem. Up until two days ago, my code for
inserting a record into a table worked fine. Update still works, but on
insert, I am getting this dialog box now:

System.FormatException: The string was not recognized as a valid DateTime.
There is a unknown word starting at index 0

It's driving me crazy. Mostly because this has worked fine until now.
There are four dates (though I removed TimeStamp to try to fix) and I have
tried everything!

One important thing to note is that my product is completely in English,
having nothing to do with other timezones or anything like that.

Anyway, I don't know if anyone can (or has time to) help. Just thought I'd
try and ask. :)

Carolyn

Here's some more information:

----------------------------------------------------------------------------
----
SQL Server Table I'm trying to add records to:
(NAME: SystemParameter)
2 SystemParameterID int 4 0 (<----Primary key with identity seed)
0 SystemParameterTypeID int 4 0
0 PersonID int 4 1
0 SystemParameterEffectiveDate datetime 8 0
0 SystemParameterEndDate datetime 8 1
0 SystemParameterValue varchar 255 1
0 LastUpdateDate datetime 8 1
1 LastUpdateUser varchar 255 1


----------------------------------------------------------------------------
----
(Stored Proc: InsertSystemParameter)
(NOTE: Removed Timestamp which didn't help)

CREATE procedure dbo.InsertSystemParameter
(
@SystemParameterID int output,
@SystemParameterTypeID int,
@PersonID int,
@SystemParameterEffectiveDate datetime = null,
@SystemParameterEndDate datetime = null,
@SystemParameterValue varchar(255) = null,
@LastUpdateDate datetime = null,
@LastUpdateUser varchar(50) = null
/*,
@TimeStamp timestamp = null*/
)
as


insert into SystemParameter
(
SystemParameterTypeID,
PersonID,
SystemParameterEffectiveDate,
SystemParameterEndDate,
SystemParameterValue,
LastUpdateUser
)
values
(
@SystemParameterTypeID,
@PersonID,
@SystemParameterEffectiveDate,
@SystemParameterEndDate,
@SystemParameterValue,
@LastUpdateUser
)
select @SystemParameterID = @@Identity
GO



----------------------------------------------------------------------------
----
(CODE - You can see where I commented out almost everything I tried!)
private void SaveSystemParameter(
int systemParameterID,
int systemParameterTypeID,
int personID,
//System.DateTime systemParameterEffectiveDate,
//System.DateTime systemParameterEndDate,
string systemParameterEffectiveDate,
string systemParameterEndDate,
string systemParameterValue,
string lastUpdateUser,
DsSystemParameter dsSystemParameter)
{

DsSystemParameter.SystemParameterRow systemParameterRow;
//-//bool staffingGuideTypeProductivityFlag = false;

//-//BcStaffingGuideType bcStaffingGuideType = new BcStaffingGuideType
(this.ContextMoniker);
//-//staffingGuideTypeProductivityFlag =
bcStaffingGuideType.IsProductivityType(staffingGuideTypeID, dsStaffingGuide)
;

//New Record?
if(systemParameterID.Equals(-1))
{
systemParameterRow = GetSystemParameterRow(systemParameterTypeID,
personID, dsSystemParameter);
}
//Existing Record
else
{
systemParameterRow = GetSystemParameterRow(systemParameterID,
dsSystemParameter);
}

//if the system Parameter wasn't located in the dataset
if (systemParameterRow == null)
{
//if new record, then create new row
if(systemParameterID.Equals(-1))
{
systemParameterRow =
dsSystemParameter.SystemParameter.NewSystemParameterRow();
}
//else could not find record
else
{
throw Utilities.GetMissingRecordException("SystemParameter");
}
}
//else, it was found, and attempting to add new, then raise an error
else if (systemParameterID.Equals(-1))
{
throw Utilities.GetDuplicateRecordException("SystemParameter");
}

//update fields

systemParameterRow.SystemParameterTypeID = systemParameterTypeID;
systemParameterRow.PersonID = personID;

//Used to work:
systemParameterRow.SystemParameterEffectiveDate =
Convert.ToDateTime(systemParameterEffectiveDate) ;

//None of these work: //-//Indicates groups of lines tried
together.

//-//CultureInfo cultureInfo = new CultureInfo("en-US");
//-//DateTime dt = DateTime.ParseExact
(systemParameterEffectiveDate, "D", cultureInfo);
//-//systemParameterRow.SystemParameterEffectiveDate = dt;

//DateTime dt = DateTime.ParseExact
(systemParameterEffectiveDate, "dd/MM/yy", null);

//-//systemParameterEffectiveDate = Convert.ToDateTime
(systemParameterEffectiveDate).ToUniversalTime().ToString( format );
//-//systemParameterRow.SystemParameterEffectiveDate =
Convert.ToDateTime(systemParameterEffectiveDate);

//systemParameterRow.SystemParameterEffectiveDate =
ParseDateTime(systemParameterEffectiveDate) ;
//systemParameterRow.SystemParameterEffectiveDate =
Convert.ToDateTime(systemParameterEffectiveDate).ToLocalTime() ;
//systemParameterRow.SystemParameterEffectiveDate =
DateTime.Parse(systemParameterEffectiveDate,
//
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat);
//systemParameterRow.SystemParameterEffectiveDate =
System.DateTime.ParseExact(systemParameterEffectiveDate, @"yyyy\/MM\/dd
HH:mm:ss", null);
//systemParameterRow.SystemParameterEffectiveDate =
System.DateTime.Parse("05/05/2005") ;
//systemParameterRow.SystemParameterEffectiveDate =
System.DateTime.Parse(systemParameterEffectiveDate) ;
if (! systemParameterEndDate.Equals(String.Empty ) )
{
systemParameterRow.SystemParameterEndDate =
Convert.ToDateTime(systemParameterEndDate); //.ToLocalTime();
}
else
{
//None of these work.
//systemParameterRow.SystemParameterEndDate =
System.DateTime.Now;
//systemParameterRow.SystemParameterEndDate =
DBNull.Value;
//systemParameterRow.SystemParameterEndDate =
Convert.ToDateTime(DBNull.Value.ToString());
//systemParameterRow.SystemParameterEndDate =
Convert.ToDateTime(String.Empty.ToString());
//systemParameterRow.SystemParameterEndDate =
DateTime.Parse(systemParameterEffectiveDate,
//
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat);

}
systemParameterRow.SystemParameterValue = systemParameterValue;

//if adding new record
if(systemParameterID.Equals(-1))
{
dsSystemParameter.SystemParameter.AddSystemParameterRow
(systemParameterRow);
}
systemParameterRow.LastUpdateUser =
lastUpdateUser;
//systemParameterRow.LastUpdateDate =
System.DateTime.Now;
//Save Record
SaveData(dsSystemParameter);
//
}

public void SaveData(DsSystemParameter dsSystemParameter)
{
_dcSystemParameter.SaveTable( dsSystemParameter );
//cb temporary removed_dwSystemParameter.SaveData(0, dsSystemParameter);
}

----------------------------------------------------------------------------
 
Hi Carolyn,

I'm a little confused. Are you saying that parsing a date has previously been fine and that you now suddently can't do it anymore?

Does DateTime.Parse("5/4/05") fail?
Does DateTime.Parse("5/4/05", new CultureInfo("en-US").DateTimeFormat) fail?

I am unable to read much out of your code sample as I don't know what systemParameterEffectiveDate is.
Sudden failure in datetime parsing mich indicate that the date is written differently, or that the current culture has changed (specifying culture should then work).
 
Hello and thanks for responding!

Yes, everything was fine until May 3rd (though it had been a couple days
since we tested this feature). With no code changes, this started
happening. Unfortunately, there is a team working together on this project
but no one can help me or seems to know what might have changed.

SystemParameterEffectiveDate is a field in the table.

Thanks so much but I just tried and neither of those worked. :(
 
The problem is fixed. We are using datasets and data containers. One of
my coworkers noticed that the Dc and the stored procedure parameters were
out of synch. No one will admit to it, though.

Oh, well. Relief!

Thanks again!
 
Back
Top