calculate Date,Week,year from database in asp.net using sql server

Posted by Dn2010 under ASP.NET on 2/22/2011 | Points: 10 | Views : 2482 | Status : [Member] | Replies : 6
hi friends,

how to calculate Date,Week,year from database in asp.net
create like this

1 year , 2 months, 1 week, 1 day

for example:

suppose registered a page before 2 months 1 week.

after 2 months 1 week, details shown.

you are registered 2 months, 1 week ago

please give in detail.

Thanks.




Responses

Posted by: Karthikanbarasan on: 2/22/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down

Hi,

Save the date when the user registered with the site. When showing in the front end use the code logic to check how many days and change it appropriately in words.



Thanks
Karthik
www.f5Debug.net

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/22/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
use this function

public static void DateDifference(DateTime registeredDate)

{
TimeSpan timeDiff = DateTime.Today.Subtract(registeredDate);
int totalDays = Convert.ToInt32(timeDiff.TotalDays);
int noOfYears = 0, noOfMonths = 0, noOfWeeks = 0, noOfDays = 0;

if (totalDays > 365)
{
noOfYears = totalDays / 365;
totalDays = totalDays % 365;
}
if (totalDays > 30)
{
noOfMonths = totalDays / 30;
totalDays = totalDays % 30;
}
if (totalDays > 7)
{
noOfWeeks = totalDays / 7;
totalDays = totalDays % 7;
}
noOfDays = totalDays;

Console.Write(string.Format("You are registered {0} years {1} months {2} weeks {3} days back",
noOfYears, noOfMonths, noOfWeeks, noOfDays));

}


you may have to change the code a bit as per your need.

Thanks and Regards
Madhu

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dn2010 on: 2/22/2011 [Member] Starter | Points: 25

Up
0
Down
thanks for your response,

how to use this function & how to retrieve date from database using this function.

thanks.



Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 2/22/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
After getting the data from the database only you need to use that function. U can get the value from the database using this link code

http://www.java2s.com/Code/ASP/ADO.net-Database/ReaddatafromdatabaseincodebehindC.htm

after getting that value pass it to the function

Thanks
Karthik
www.f5Debug.net

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/23/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
If you want to do your calculation before loading the data then you need to write a SQL function similar to this and use it.

Thanks and Regards
Madhu

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/23/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
any example of sql function??

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response