Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Team,

it a very simple question but i dont know why its happening.
I want current Date and Time in C#.
I am Trying this:
customerLoanRecord.CreatedOn = DateTime.Now.Date;

I am getting the date but i cant see the time .So for Time what should i do .
i required both DateandTime.

Thank
harshal raut
Posted
Comments
EduChapow 29-Jan-14 6:04am    
our friend OriginalGriff aswer your question, but you need search better and more man!
[no name] 29-Jan-14 6:35am    
ohh great

Take off the word "Date":
C#
customerLoanRecord.CreatedOn = DateTime.Now;

The Date property of a DateTime object removes all time information!
 
Share this answer
 
Comments
[no name] 29-Jan-14 6:03am    
Thanks for tour quick response.
But if i use customerLoanRecord.CreatedOn = DateTime.Now;
i am getting the value null.What shou;d i do?

Thanks HArshal
OriginalGriff 29-Jan-14 6:06am    
DateTime.Now can't return a null - (if only because if it did your previous code would crash, instead of showing just the Date part).
So you need to show us the definition of your CreatedOn property.
CPallini 29-Jan-14 6:06am    
Damned fast-typing-man, have my 5.
OriginalGriff 29-Jan-14 6:07am    
I knew that typing course would come in handy one day! :laugh:
[no name] 29-Jan-14 6:12am    
hey right.
the definition of CreatedOn property is datetime.
till i am facing the problem .What to do?
Quote:
customerLoanRecord.CreatedOn = DateTime.Now.Date;
This should be:
customerLoanRecord.CreatedOn = DateTime.Now;


(Assuming customerLoanRecord.CreatedOn type is DateTime)
 
Share this answer
 
Comments
[no name] 29-Jan-14 6:12am    
hey right.
the definition of CreatedOn property is datetime.
till i am facing the problem .I am getting null value .What to do?
Declare CreatedOn as a property and then check.

C#
private DateTime _CreatedOn = new DateTime();
public DateTime CreatedOn
{
     get
     {
         _CreatedOn = DateTime.Now;
         return _CreatedOn;
     }
     set
     {
         _CreatedOn = value;
     }
}
 
Share this answer
 
Comments
[no name] 29-Jan-14 6:28am    
i have the property
public DateTime? CreatedOn { get; set; }
then to i am facing the issue.Please help.
CPallini 29-Jan-14 6:38am    
My test:
Program prg = new Program();
prg.CreatedOn = DateTime.Now;
Console.WriteLine(prg.CreatedOn.ToString());
The output:
29/01/2014 12.36.49
[no name] 29-Jan-14 6:58am    
i tried your solution but i am getting the message.
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

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