Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Date format issue

  Asked By: Ervin    Date: Apr 22    Category: Sharepoint    Views: 2120

I have developed a custom web part responsible for
copying a source file (from an Area/doc lib) to a
destination. The project was developed in Visual
Studio.NET 2003 using C# as the language.

The web part is responsible for copying the source
file and the contents of the source file metadata.
Previous to ‘Shareponint Portal Service Pack 1’ I did
not have any issues with copying the datetime type
columns to the destination; now, since we have
upgraded to Service Pack1, I’m encountering the
following error when trying to copy the datetime type
to the destination file column:
""Error Converting date: String was not recognized as
a valid DateTime."

I’m looping through the file properties of the
destination file in the code. If the destination file
property is a ‘datetime’ type then I copy the contents
of the source property (stored in the
‘CurrentFileProps’ hash table) to the destination.
Here’s the section of code responsible for copying the
source file to a destination datetime field type.


else if (FileField.Type.Equals(SPFieldType.DateTime))
{
try
{
SPFieldDateTime FieldDate =
(SPFieldDateTime)FileField;
FieldDate.DefaultValue =
CurrentFileProps[FileField.Title.ToString()].ToString();
FieldDate.DisplayFormat =
SPDateTimeFieldFormatType.DateOnly;

DestFile.Item[FileField.Title.ToString()] =
System.Convert.ToDateTime(CurrentFileProps[FileField.Title.ToString()].ToString(\
));
DestFile.Item.Update();

}
catch(Exception ExDTConv1)
{
Response.Write("Error Converting date
title: "+ExDTConv1.Message.ToString()+"
"+ExDTConv1.InnerException.ToString());
Response.Write("string:
"+ThisDate.ToString());
}

I have tried other methods, such as converting the
source file property string to a datetime, and I’m
still getting the error described above. I need to
know if there’s a “Proper” way to copy the contents of
the source datetime field to the destination datetime
field. If so, how can I do it successfully?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Horace Coffey     Answered On: Apr 22

A piece from my ToolBox, although in C#, you should be
able to find your solution:


// v0.1 - convert a date  for sql storage (default was
"MM/dd/yyyy")
public string DateTime_GetConverted(DateTime
dDate,string sFormat)
{
return dDate.ToString(sFormat,
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

// v0.1 - return the age between a given date and now
public double DateTime_GetAgeInHours(DateTime bDate)
{
DateTime aDate = DateTime.Now;

TimeSpan ts = (TimeSpan) aDate.Subtract(bDate);

return ts.TotalHours;
}

// v0.1 - return the number of days between 2 dates
public double DateTime_GetDaysFromTo(DateTime
dtFrom,DateTime dtTo)
{
TimeSpan ts = (TimeSpan) dtTo.Subtract(dtFrom);

return ts.TotalDays;
}

// v0.1 - retrieve the current date
public string DateTime_GetCurrent()
{
DateTime dt = DateTime.Now;
return dt.ToString("dd-MM-yyyy HH:mm",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

// v0.1 - retrieve the current date for sql storage
public string DateTime_GetCurrentForSQL()
{
DateTime dt = DateTime.Now;
return dt.ToString("MM/dd/yyyy HH:mm:00",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

// v0.1 - convert the date for the popup calendar in
format (MM-dd-yyyy)
public string DateTime_GetDateForCalendar(string
sDate,double dAddDays,string sFormat)
{
DateTime dt = Convert.ToDateTime(sDate);
dt = dt.AddDays(dAddDays);

return dt.ToString(sFormat,
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

// v0.1 - convert a date for sql storage (default was
"MM/dd/yyyy")
public string DateTime_GetDateForSQL(string
sDate,string sFormat)
{
DateTime dt = Convert.ToDateTime(sDate);
return dt.ToString(sFormat,
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

// v0.1 - convert a date
public string DateTime_GetDate(string sDate)
{
DateTime dt = Convert.ToDateTime(sDate);
return dt.ToString("dd/MM/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
}

 
Didn't find what you were looking for? Find more on Date format issue Or get search suggestion and latest updates.




Tagged: