Timespan

VBdotnet2005
VBdotnet2005 used Ask the Experts™
on
I want to find the different between two dates.
var TakenDate = info.DateTime;

TakenDate is 2010:11:20 18:54:32

TimeSpan diff = DateTime.Now - TakenDate;  < error on Takendate
int date_count = diff.Days;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Imran Javed ZiaConsultant Software Engineer - .NET Architect

Commented:
Please try folllowing

DateTime TakenDate = info.DateTime;
TimeSpan diff = DateTime.Now - TakenDate;  < error on Takendate
int date_count = diff.Days;


if you get error on DateTime TakenDate = info.DateTime;
then use following

DateTime TakenDate = DateTime.Parse( info.DateTime);
First make sure that TakenDate is actually a DateTime type. If it's a String then try parse it first (DateTime.Parse).
Secondly, you should use diff.TotalDays to determine the total number of days difference in the time span not Days.

Without specific error details it's hard to determine your problem. Also using var means we can't immediately see what the type is getting returned by info.DateTime

Author

Commented:


var TakenDate = info.DateTime;

TakenDate is 2010:11:20 18:54:32  < this is a string? This is what it provides

I tried


DateTime TakenDate = DateTime.Parse( info.DateTime);

same error


HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
string TakenDate = info.DateTime;
TakenDate is 2010:11:20 18:54:32   <  is a string

Author

Commented:
can I convert to datetime and get totaldays?

string TakenDate = info.DateTime;
TakenDate is 2010:11:20 18:54:32

Author

Commented:
Still errors

string TakenDate = info.DateTime;  // TakenDate is 2010:11:20 18:54:32  
TimeSpan diff = DateTime.Now.Subtract(Convert.ToDateTime(TakenDate));
 int date_count = diff.Days;
DateTime TakenDate = DateTime.Parse(info.DateTime);
double day_count = (DateTime.Now - TakenDate).TotalDays;

Open in new window

Imran Javed ZiaConsultant Software Engineer - .NET Architect

Commented:
Please try this

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);

Author

Commented:
FormatException

DateTime TakenDate = DateTime.Parse(info.DateTime);
double day_count = (DateTime.Now - TakenDate).TotalDays;
Consultant Software Engineer - .NET Architect
Commented:
Please try this

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);

or

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
You may have to use ParseExact because your string isn't a standard DateTime string:
 
DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:M:d HH:mm:ss", CultureInfo.InvariantCulture);

Open in new window

The format string may have to be changed depending on what your dates look like with single digit months and days.

Author

Commented:
This works... : )
Thanks guys

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
Imran Javed ZiaConsultant Software Engineer - .NET Architect

Commented:
You are Most Welcom

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial