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.
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.
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");
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.
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);
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/
string StringValue = MyDate.HasValue ? MyDate.Value.ToString("dd.MM.yyyy HH:mm:ss") : string.Empty;
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.
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.)
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