Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am retrieve date from one table and converted that string value to DateTime format,and then i add 2 months in that date value,but it add month value in date place.
For Eg:
Date is 03/09/2015 --- dd/MM/yyyy format
no of months is 2, if i add month to that date it became 05/09/2015,but correct one is 03/11/2015.
How to achieve this ?

Here is my program code

VB
Dim splitstrcompdetails As String() = strcompdetails.Split(":") ' In splitstrcompdetails we get Aravind:3/9/2015
Dim strRegDate As DateTime = Convert.ToDateTime(splitstrcompdetails(1)).ToString(stringDateFormat) 'here stringDateFormat is dd-MMM-yyyy and we get strRegDate as 3/9/2015
Dim strTotMonths As Integer = Convert.ToInt32(strserialdetails) + Convert.ToInt32(expirymonth.Value) 'her strserialdetails is 2 and expirymonth.Value is 3 so strTotMonths is 5
Dim strExpDate As Date = strRegDate.AddMonths(Convert.ToInt32(strTotMonths))' but here we get strExpDate as 8/9/2015  but correct value is 3/2/2016


Pls reply asap

Regards
Aravind
Posted

1 solution

Have a look at example:

VB
Dim strcompdetails As String = "Aravind:3/9/2015"
Dim stringDateFormat As String = "dd-MMM-yyyy"
Dim provider As System.Globalization.CultureInfo = new System.Globalization.CultureInfo("de-DE")
Dim splitstrcompdetails As String() = strcompdetails.Split(":")
Dim RegDate As DateTime = Convert.ToDateTime(splitstrcompdetails(1), provider).AddMonths(5)

Console.WriteLine("{0}", RegDate.ToString(stringDateFormat)) 'return 03-Feb-2016


As you can see, you need to use CultureInfo class[^] to get/set specific culture to be able to get proper result.
Table of Language Culture Names, Codes, and ISO Values Method[^]
 
Share this answer
 
Comments
Aravindba 3-Sep-15 4:02am    
hiwhen retive time also i use provider not success,still adding no of months to date field
Dim provider As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")

Dim strRegDate As DateTime = Convert.ToDateTime(splitstrcompdetails(1), provider).ToString(stringDateFormat)

Dim strExpDate As Date = Convert.ToDateTime(strRegDate, provider).AddMonths(Convert.ToInt32(strTotMonths))

no of months is 6 ,strRegDate is 3/9/2015

strExpDate is 9/9/2015,but expect is 3/3/2016
Maciej Los 3-Sep-15 5:37am    
You're using wrong provider. Have you tested with CultureInfo("de-DE")? ISO standard date format is: "yyyy-MM-dd"
Aravindba 3-Sep-15 8:07am    
yes working fine,thanx
Maciej Los 3-Sep-15 8:12am    
You're very welcome ;)

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