tags:

views:

815

answers:

11

I have a DateTime? field. Its currently storing UT.

I would like it to store time in the format I provide. In this case:

 "dd.MM.yyyy HH:mm:ss"

How can I change the value of the DateTime? field to hold a formatted value instead.

+1  A: 

Do you mean this:?

string s = myDate.ToString("dd.MM.yyyy HH:mm:ss");

or for a nullable DateTime:

string s = myDate == null ? null : myDate.Value.ToString("dd.MM.yyyy HH:mm:ss");
Philippe Leybaert
Yes, but it doesn't work on a DateTime? Only on a DateTime
JL
Try myDate.Value.ToString, ensuring that it is not null first.
RichardOD
Or more specifically myDate.Value.ToString("dd.MM.yyyy HH:mm:ss")
RichardOD
Wouldn't string.Empty be better than null for the string value where the DateTime is null?
Stevo3000
Define "better". It all depends on what your requirements are.
Philippe Leybaert
@Philippe Leybaert - In this instance 'better' means the string can be used as a string without another operation to determine if it's null. Especially as the OP is asking about displaying the string.
Stevo3000
+1  A: 

Store it as string.

x2
Can't change the data type its coming from a web service... if possible really need to work with this DateTime? data type...
JL
@JL, actualy DateTime stored as one long number of ticks. What you see is just formatting string, so you have to format it too.
x2
+4  A: 

You don't alter how the time is stored, you alter the time representation by using the methods that convert the DateTime to string (in your case theDate.Value.ToString("dd.MM.yyyy HH:mm:ss");).

If you want something that always yielded the format you have specified, you could write your own date wrapper class.

RichardOD
Good answer, especially the comment about storage! +1
Cerebrus
I don't think the OP means 'storage' as the value is coming from a web service. I think he means how the DateTime in the field (DataView maybe) is displayed!
Stevo3000
+1  A: 

You cannot change the value of the date itself, but you can vary the format used to convert it to a string.

Try the following command:

String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
David Andres
+1  A: 

Very Easy. Save the date in a string as shown below:

String date = DateTime.Now.Date; date.ToString("dd.MM.yyyy HH:mm:ss");

Cheers.. http://www.naresh.se/

+2  A: 
string StringValue = MyDate.HasValue ? MyDate.Value.ToString("dd.MM.yyyy HH:mm:ss") : string.Empty;
Stevo3000
+2  A: 

Ah! DateTime questions... everyone's favourite!! ;-)

The only relevant point to this question is that as @x2 mentioned in the comments, a DateTime (nullable or otherwise) is stored as a number of ticks. What you see in the Watch window is just a abstraction of that value into a human readable date and time component.

Why would you want to store a DateTime object as formatted ? Wouldn't that defeat the purpose of having a DateTime variable ? "Formatting", by definition means conversion to a string. This means creation of a separate string variable which holds the Datetime value formatted as per your requirement. In one of the comments, you say you need to work with this variable... why not create a formatted string variable from this source DateTime variable, instead.

Cerebrus
+1  A: 

See this expample of Date time formatting in C#

using System; 
using System.Globalization;
class DateAndTimeFormatting 
{
  static DateTime dt = DateTime.Now; 
  static void Main()
  {
    ShowFormatting(DateTimeFormatInfo.InvariantInfo, "InvariantInfo"); 
    ShowFormatting(DateTimeFormatInfo.CurrentInfo, "CurrentInfo"); 
  }
  static void ShowFormatting(DateTimeFormatInfo format, string strLabel) 
  {
    Console.WriteLine(strLabel);
    Console.WriteLine(new string('-', strLabel.Length)); 
    string[] strFormats = {"d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y" }; 
    foreach (string strFormat in strFormats) 
       Console.WriteLine("{0}: {1}", strFormat, dt.ToString(strFormat, format)); 
    Console.WriteLine();
  }
}

Notice the strFormats array in the ShowFormatting method. That array contains the formatting strings you can use in the ToString method. (You can use those same letters in the placeholders in the formatting string of Console.WriteLine.) The program first shows the formatting for DateTimeFormatInfo.InvariantInfo:

InvariantInfo 
d: 12/02/2006
D: Saturday, 02 December 2006 
f: Saturday, 02 December 2006 16:48 
F: Saturday, 02 December 2006 16:48:43 
g: 12/02/2006 16:48 
G: 12/02/2006 16:48:43 
m: December 02 
r: Sat, 02 Dec 2006 16:48:43 GMT 
s: 2006-12-02T16:48:43 
t: 16:48 
T: 16:48:43 
u: 2006-12-02 16:48:43Z 
U: Saturday, 02 December 2006 21:48:43 
y: 2006 December

The following formatting is shown for DateTimeFormatInfo.CurrentInfo:

CurrentInfo 
d: 12/2/2006 
D: Saturday, December 02, 2006
f: Saturday, December 02, 2006 4:48 PM 
F: Saturday, December 02, 2006 4:48:43 PM 
g: 12/2/2006 4:48 PM 
G: 12/2/2006 4:48:43 PM 
m: December 02
r: Sat, 02 Dec 2006 16:48:43 GMT 
s: 2006-12-02T16:48:43 
t: 4:48 PM 
T: 4:48:43 PM 
u: 2006-12-02 16:48:43Z 
U: Saturday, December 02, 2006 9:48:43 PM 
y: December, 2006

When the uppercase and lowercase letters produce different result (such as d and D)the uppercase letter produces a longer string. For the r, R, s, or u formatting strings, the results are the same regardless of the second argument to ToString. (You can also define your own formatting.)

Sauron
Things like this are useful- http://john-sheehan.com/blog/wp-content/uploads/msnet-formatting-strings.pdf
RichardOD
+1  A: 

I'll add my one to the list and the reference I use, which is as short as I can get it:

--- Default formats ---  

Tokens:  
dD tT fF gG rsu M Y  

d = short/long [d]ate  
t = short/long [t]ime  
f = short/long [f]ull  
g = short/long [g]eneral or default  
rsu = RFC or ISO format, last 2 are sortable:
r: Fri, 20 Mar 2009 00:00:00 GMT (RFC1123)
s: 2009-03-20T00:00:00 (ISO 8601)
u: 2009-03-20 00:00:00Z (RFC1123)
M = day & month  
Y = month & year  

--- Custom formats ---  

dd ddd dddd  
15 Mon Monday  

MM MMM MMMM  
06 Jun June  
yy yyyy  
08 2008  

hh HH  
01 13  
mm  
ss  

tt  
AM or PM
Chris S