Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on 

Timespan

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;
.NET ProgrammingC#

Avatar of undefined
Last Comment
Imran Javed Zia
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

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);
Avatar of brutaldev
brutaldev
Flag of South Africa image

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
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER



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


Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

string TakenDate = info.DateTime;
TakenDate is 2010:11:20 18:54:32   <  is a string
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

can I convert to datetime and get totaldays?

string TakenDate = info.DateTime;
TakenDate is 2010:11:20 18:54:32
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

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;
Avatar of brutaldev
brutaldev
Flag of South Africa image

DateTime TakenDate = DateTime.Parse(info.DateTime);
double day_count = (DateTime.Now - TakenDate).TotalDays;

Open in new window

Please try this

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

FormatException

DateTime TakenDate = DateTime.Parse(info.DateTime);
double day_count = (DateTime.Now - TakenDate).TotalDays;
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of brutaldev
brutaldev
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

This works... : )
Thanks guys

DateTime TakenDate = DateTime.ParseExact(info.DateTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
You are Most Welcom
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo