Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
using DateTime.
Can i declare DateTime? like:
C#
int intnumber ;

can we write:
C#
DateTime dtdate ;

and value it, like:
C#
dtdate = 6/18/2012 09:54:22 ;

i want to read a text like "6/18/2012 09:54:22" from input.
then save it into a file.
then read it from that file, and have it not as an string have it as a DateTime.
and what are DateTime method's like:
C#
DateTime.Now
?
Posted
Updated 31-May-21 6:31am
v2

You can create a new DateTime value in any of the following ways:

By calling any of the overloads of the DateTime constructor that allow you to specify specific elements of the date and time value (such as the year, month, and day, or the number of ticks).
The following statement illustrates a call to one of the DateTime constructors to create a date with a specific year, month, day, hour, minute, and second.
C#
DateTime date1 = new DateTime(2008, 5, 1, 8, 30, 52);


By assigning the DateTime object a date and time value returned by a property or method.
The following example assigns the current date and time, the current Coordinated Universal Time (UTC) date and time, and the current date to three new DateTime variables.
C#
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;
DateTime date3 = DateTime.Today;


By parsing the string representation of a date and time value.
The Parse, ParseExact, TryParse, and TryParseExact methods all convert a string to its equivalent date and time value.
The following example uses the Parse method to parse a string and convert it to a DateTime value.
C#
string dateString = "19/6/2012 10:51:52 AM";
DateTime date1 = DateTime.Parse(dateString, 
                          System.Globalization.CultureInfo.InvariantCulture);


By calling the DateTime structure's implicit default constructor. (For details on the implicit default constructor of a value type, see Value Types (C# Reference).) An approximate equivalent, for compilers that support it, is declaring a DateTime value without explicitly assigning a date and time to it.
The following example illustrates a call to the DateTime implicit default constructor in C# and Visual Basic, as well as a DateTime variable declaration with no assignment in Visual Basic.
C#
DateTime dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));
// The following method call displays True.
Console.WriteLine(dat1.Equals(DateTime.MinValue));

Ref.:DateTime Structure[^]

Also have look on some more useful links:
C# DateTime[^]
C# DateTime Format[^]

Some more:
String Format for DateTime [C#][^]
 
Share this answer
 
Comments
mrmarzban 23-Jun-12 7:36am    
error while parsing:
String was not recognized as a valid DateTime.
 
Share this answer
 
look This[^]

This[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900