Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am doing a project and I got confused in an area that how to convert the date time to string value?

controller

var FromDate = DateTime.Now.AddDays(-1);
                FromDateString = FromDate.ToString();


I am getting correct value to fromdate but it is not passing to FromDateString. This FromDateString have given in parameter of an action method and getting value.
My only problem is that how can I convert this datetime to string ??
FromDateString is given as string and this cannot be changed. Is it possible to do that ?
can anyone please help me to correct my code ??

What I have tried:

have searched for code but didn't get how to write in controller .
Posted
Updated 24-May-17 20:37pm
Comments
Maciej Los 25-May-17 2:04am    
Why do you need to convert date to string?
Do you want to "convert" date to specific format, such as: ddMMyyyy or MMddyyyy or yyyyMMdd?
What's your problem?
What you mean by saying: "cannot be changed"?
ammu11 25-May-17 2:13am    
No I var FromDate I am getting the value that is datetime , this datetime in FromDate, I need to get this value to FromDateString, but it is showing as null. How can I get value to FromDateString ??
Maciej Los 25-May-17 2:19am    
Sorry, but i don't get you... Can you be more specific and provide example.
FromDateString returns to me "2017-05-24 08:20:12". It isn't null.
ammu11 25-May-17 2:26am    
var FromDate = DateTime.Now.AddDays(-1);
when I give this I am getting yesterdays datetime to FromDate but
when I give this
FromDateString = FromDate.ToString();
the value in FromDate is not passing to FromDateString. Why is that ?? currently FromDateString is in string datatype . I need to passthe value in FromDate to FromDateString. Is that possible ?? When I give I am getting null value .
Maciej Los 25-May-17 2:29am    
As i told you, i'm getting correct value. Why you don't - i have no idea. Use debugger.

1 solution

The code you show puts a formated string version of yesterday's date into FromDateString - it can't do anything else, and will do so every time you execute that code.

And there is the rub: for FromDateString to be null means that you haven't executed that code at all! Why not? We don't know - we have no idea how the rest of your code works.

So start with the debugger, and put a couple of breakpoints in there. The first one goes on the lines you show above, and the other(s) go when you use FromDateString for anything: passing it to a method, printing it, every where.
Now run your code, and see if the code you show here is executed before any other access to the variable. If it isn't, then you need to look at your code and find out why not - we can't do that for you!
If it is, then you need to look at the definition of FromDateString - is it a local variable that masks a class level variable of the same name and then means you never set the class level version? Again - we can't tell!
 
Share this answer
 
Comments
ammu11 25-May-17 3:15am    
@OriginalGriff I have done as you said by changing some code.
code is

string FromDate = DateTime.Now.AddDays(-2).ToString();
string Todate = DateTime.Now.ToString();

if (!String.IsNullOrEmpty(Todate.ToString()) && !String.IsNullOrEmpty(FromDate.ToString()))
{
BookingList = BookingList.Where(s => s.DateTime >= FromDate.ToDateFromString() && s.DateTime <= Todate.ToDateFromString()).ToList();
}

I have changed the code to this, why because only need to show last two days data and in db it has three fields and my problem here is that it is not showing and the count in BookingList is 0. Why is that ??
OriginalGriff 25-May-17 3:38am    
Why on earth are you playing with strings?
When you do string comparisons, it compares them character by character - and the first different character determines the comparison for the whole string.
So depending on what the locale and default Long and Short date formats for the specific PC you are using (and what format the POC that inserted the date string into the DB is set to) the comparison could return any "greater than" and "less than" results.
Use the DateTime values directly, and make sure that your DB DateTime values are also DateTime, not string.

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