Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I got error from when i convert string to double.

see my code
------------
Dim week As String = getWeek(DateTime.Now) 'To get current week
week = Convert.ToDouble(week).ToString ' To convert string to double
Dim Cntweek As String = week & "-" + DateTime.Now.Year 'need to display "28-2011"


But, getting conversion error in CntWeek...?
Posted
Updated 15-Jul-11 3:13am
v2
Comments
Tarun.K.S 15-Jul-11 9:07am    
Use DateTime.Now.Year.ToString()
Dim Cntweek As String = week & "-" & DateTime.Now.Year.ToString()
R. Giskard Reventlov 15-Jul-11 9:09am    
You should add that as the solution.
Tarun.K.S 15-Jul-11 9:11am    
I think you are right! :D

Do it like this :

VB
Dim week As String = getWeek(DateTime.Now) 'To get current week
week = Convert.ToDouble(week).ToString 
Dim Cntweek As String = week & "-" & DateTime.Now.Year.ToString() 'need to display "28-2011"


Update: There is an interesting observation made by Manfred regarding the second line. You may not be needing it. Please do check again if you really need to use the second line.
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 15-Jul-11 9:18am    
Please have a hard look at the second line of code. Why would anybody convert a string to a double and then back to string.
Please correct this abomination. :)
BTW: I haven't voted yet.
Tarun.K.S 15-Jul-11 9:26am    
Aah yes I thought for a while, and I came to the conclusion that there must be some kind of requirement for the OP to do that conversion. Anyway I will update the answer removing the second line.
R. Giskard Reventlov 15-Jul-11 9:27am    
Good point though sometimes (rightly or wrongly) it seems that it's best to answer the question as asked: for all we know this is an exercise that the op has been asked to do.
Why don't you get the current week this way:

VB
Dim week as Integer = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(DateTime.Now.DayOfYear) / 7)
Dim CntWeek as String = string.Format("{0} - {1}", week, DateTime.Now.Year)


EDIT =============

I would create an extension method like so (this is C#, but shouldbe easy to convert to VB). Then you could use it for any date you wish instead of just today's date:

C#
public static int WeekOfYear(this DateTime date)
{
    int week = (int)Math.Ceiling((double)date.DayOfYear / 7);
    return week;
}
 
Share this answer
 
v3
C#
Dim week As String = getWeek(DateTime.Now)
Dim Cntweek As String = String.Format('{0}-{1}', week, DateTime.Now.Year)
 
Share this answer
 
v3

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