Print Page | Close Window

Converter for Time Only

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=267
Printed Date: 31-Aug-2025 at 4:25am


Topic: Converter for Time Only
Posted By: Customer
Subject: Converter for Time Only
Date Posted: 16-Jul-2007 at 11:15am
1) I notice that the default DevForce DataConverters has both a

DataConverter.ReadOnlyDateTime and DataConverter.ReadOnlyDate

but there's no DataConverter.ReadOnlyTime

Would you have a code snippet for a custom converter? We have a Converters class for our various custom converters (mostly list converters).

I'm trying to do something like this:

XtraGridBindingDescriptor bdLastModDttm = appointmentXtraGridBM.Descriptors.Add("LastModDttm", prefix + ActivityLog.LastModDttmEntityColumn.ColumnName, DataConverter.ReadOnlyDateReadOnlyDateTime);

prefix + ActivityLog.LastModDttmEntityColumn.ColumnName is a DateTime property that I simply want to display in my grid as a Time value.




Replies:
Posted By: IdeaBlade
Date Posted: 16-Jul-2007 at 11:16am
Try this:
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp
 
 
.NET Framework Developer's Guide  

A standard DateTime format string consists of a single format specifier character from the following table. If the format specifier is not found in the table below, a runtime exception is thrown. If the format string is longer than a single character (even if the extra characters are white spaces), the format string is interpreted as a custom format string.

Note that the result string produced by these format specifiers are influenced by the settings in the Regional Options control panel. Computers with different cultures or different date and time settings will generate different result strings.

The date and time separators displayed by format strings are defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassDateSeparatorTopic.asp - DateSeparator and http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassTimeSeparatorTopic.asp - TimeSeparator characters associated with the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassTopic.asp - DateTimeFormat property of the current culture. However, in cases where the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationCultureInfoClassInvariantCultureTopic.asp - InvariantCulture is referenced by the 'r', 's', and 'u' specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change based on the current culture.

The following table describes the standard format specifiers for formatting the DateTime object.

Format specifier Name Description
d Short date pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassShortDatePatternTopic.asp - DateTimeFormatInfo.ShortDatePattern property associated with the current thread or by a specified format provider.
D Long date pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassLongDatePatternTopic.asp - DateTimeFormatInfo.LongDatePattern property associated with the current thread or by a specified format provider.
t Short time pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassShortTimePatternTopic.asp - DateTimeFormatInfo.ShortTimePattern property associated with the current thread or by a specified format provider.
T Long time pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassLongTimePatternTopic.asp - DateTimeFormatInfo.LongTimePattern property associated with the current thread or by a specified format provider.
f Full date/time pattern (short time) Displays a combination of the long date and short time patterns, separated by a space.
F Full date/time pattern (long time) Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassFullDateTimePatternTopic.asp - DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider.
g General date/time pattern (short time) Displays a combination of the short date and short time patterns, separated by a space.
G General date/time pattern (long time) Displays a combination of the short date and long time patterns, separated by a space.
M or m Month day pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassMonthDayPatternTopic.asp - DateTimeFormatInfo.MonthDayPattern property associated with the current thread or by a specified format provider.
R or r RFC1123 pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassRFC1123PatternTopic.asp - DateTimeFormatInfo.RFC1123Pattern property associated with the current thread or by a specified format provider. This is a defined standard and the property is read-only; therefore, it is always the same regardless of the culture used, or the format provider supplied. The property references the CultureInfo.InvariantCulture property and follows the custom pattern "ddd, dd MMM yyyy HH:mm:ss G\MT". Note that the 'M' in "GMT" needs an escape character so it is not interpreted. Formatting does not modify the value of the DateTime; therefore, you must adjust the value to GMT before formatting.
s Sortable date/time pattern; conforms to ISO 8601 Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassSortableDateTimePatternTopic.asp - DateTimeFormatInfo.SortableDateTimePattern property associated with the current thread or by a specified format provider. The property references the CultureInfo.InvariantCulture property, and the format follows the custom pattern "yyyy-MM-ddTHH:mm:ss".
u Universal sortable date/time pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassUniversalSortableDateTimePatternTopic.asp - DateTimeFormatInfo.UniversalSortableDateTimePattern property associated with the current thread or by a specified format provider. Because it is a defined standard and the property is read-only, the pattern is always the same regardless of culture or format provider. Formatting follows the custom pattern "yyyy-MM-dd HH:mm:ssZ". No time zone conversion is done when the date and time is formatted; therefore, convert a local date and time to universal time before using this format specifier.
U Universal sortable date/time pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassFullDateTimePatternTopic.asp - DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider. Note that the time displayed is for the universal, rather than local time.
Y or y Year month pattern Displays a pattern defined by the http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemGlobalizationDateTimeFormatInfoClassYearMonthPatternTopic.asp - DateTimeFormatInfo.YearMonthPattern property associated with the current thread or by a specified format provider.
Any other single character Unknown specifier    

The following example illustrates how to use the standard format strings with DateTime objects.

[Visual Basic]
Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")

' Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"

' Use the DateTimeFormat from the culture associated 
' with the current thread.

Console.WriteLine( dt.ToString("d") )  
Console.WriteLine( dt.ToString("m") )

' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )

' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )
[C#]
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");

// Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";

// Use the DateTimeFormat from the culture associated 
// with the current thread.
Console.WriteLine( dt.ToString("d") );  
Console.WriteLine( dt.ToString("m") );

// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );

// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );

// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );


 




Print Page | Close Window