convert date format MMMM yyyy to yyyyMM in asp.net vb - asp.net

i have textbox that contain string month and year value, example: January 2010
but i want to save it into mysql database with format 201001 --->year and then number of month
how can i convert this string?

Based from this link, maybe you can try this:
Dim dateval As String = "January 2010"
Dim year As String = dateval.Split(" "C)(1)
Dim month As String = DateTime.ParseExact(dateval.Split(" "C)(0), "MMMM", CultureInfo.CurrentCulture).Month.ToString()
Dim yourdateval As String = String.Concat(year, month)

Related

Get the values from a string

I need help with a quick question I a string "07/10/2014" how can I get first the year "2014",second the month "10" ,third the day- "07" with out "/" only the values in VB.NET
Please show me the full way how to do it.
First declare it like this Dim x as Date = "07/10/2014". And to get the individual values use x.Day, x.Month and x.Year
Use the DateTime.Parse() method then use the return DateTime structure to extract the Month, Day, Year properties (similar to DJK's answer above).
If the thread's current culture is set to one that understands "mm/dd/yyyy" format, then the code can be as simple as:
Dim dt As DateTime = DateTime.Parse("07/15/2014")
MessageBox.Show(String.Format("Month: {0}; Day: {1}; Year: {2}", dt.Month, dt.Day, dt.Year))
Have a look at this.
Dim MyDate As Date
MyDate = "07/10/2014"
MsgBox(Format(MyDate, "dd")) ' dd gives you day number
MsgBox(Format(MyDate, "MM")) ' MM gives you month number
MsgBox(Format(MyDate, "YYYY")) ' YYYY gives you year number
The full list of date fomatting string could be found here (MSDN)
UPDATE
Use following example to assign to a string variable
Dim DayOfString As String DayOfString
DayOfString = Format(MyDate, "dd")

How do I convert a string date saved in seperate variables to a date using asp.net vb? The month variable is written out in text format

The data that I have to convert is written as the separate variables "Month", "Day", and "Year".
The data I need to convert for example is:
Month is "July"
Day is "21"
Year is "2013"
Combine it and use DateTime.ParseExact with CultureInfo.InvariantCulture:
Dim dtStr = String.Format("{0} {1} {2}", month, day, year)
Dim dt = Date.ParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture)
Also have a look at: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you don't know if the data is valid you should use DateTime.TryParseExact:
Dim dt As DateTime
If Date.TryParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
Console.Write("Date is: " & dt.ToShortDateString())
End If
I would first put the date into a string so
Dim dateString as string = string.format({0} {1} {2}, day, month, year)
Where day, month, year reference the variables holding the date values.
Then try
Dim result as DateTime = Convert.ToDateTime(dateString)

Date format issue in vb.net

I am trying to parse a date from a textbox and store it in a date variable
Dim enddt_2 As Date = Date.ParseExact(txtenddt.Text, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo) 'txtenddt.Text
expenddt_1 = enddt_2.AddDays(-1)
enddt = enddt_2.ToString("dd/MM/yyyy")
enddt is a Date variable and when i convert enddt_2 to a string i get the error as
Conversion from string "17/01/2012" to type 'Date' is not valid.
Let me clarify, if a value in textbox is 17/01/2012 than after parsing the value is changed to 01/17/2012 (my systems Region and Language are dd/MM/yyyy) in enddt_2 and when i try to convert to dd/MM/yyyy format and store into a date variable i get the above error. This error comes only for the dates after 12. i.e a date variable accepts a date in MM/dd/yyyy format.The dates before 12 work fine, i.e for all dates from 1 to 12 there is no error.
How can i make enddt store the date in dd/MM/yyyy format.
The enddt is Date variable and you can't assign string value in it and do not change your regional settings or even date/time format.
Change type of enddt if you want to store string date.
Dim enddt as String = enddt_2.ToString("dd/MM/yyyy")
did you try this?
Dim enddt_2 As Date = DateTime.ParseExact("17/01/2012", "dd/MM/yyyy", _
System.Globalization.CultureInfo.InvariantCulture)
Dim newD As Date = enddt_2.AddDays(-1)
Dim xStr As String = "Original Date: " & enddt_2.ToString("dd/MM/yyyy") & vbCrLf
xStr &= "FormatedOriginalDate: " & enddt_2.ToString("MM/dd/yyyy") & vbCrLf
xStr &= "NewDate: " & newD.ToString("MM/dd/yyyy")
Console.Writeline(xStr)
Output:
Original Date: 17/01/2012
FormatedOriginalDate: 01/17/2012
NewDate: 01/16/2012

Date format in vb.net

I am using the following function to Convert a String variable to a Date. Subtract a day from it and convert the date back to a String. The code goes as follows
Dim edate As String
Dim expenddt As Date
edate = txtenddt.Text
expenddt = Date.ParseExact(edate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
expenddt = expenddt.AddDays(-1)
Dim asd As String = expenddt.ToString
If edate has a value 29/12/2011 than the value in expenddt gets changed to a different format and the value in expenddt comes to 12/29/2011 and later after subtracting a day expenddt is 12/28/2011 and than when i convert it back to a String i get the value in asd as "12/28/2012 12:00:00 AM"
I have changed the date format on my system to d/M/yyyy in Regional And Language Option in Control Panel but i still get a different format in expenddt
Can anyone explain me why this is happening? How can i keep the format of the date in dd/mm/yyyy e.g 29/12/2011 and after
Subtracting a day it should remain 28/12/2011 and not 12/29/2011
Use Date.ToString() to convert date to string with dd/MM/yyyy format.
Dim asd As String = expenddt.ToString("dd/MM/yyyy")
Just try this:
Dim dateString As String = "Mon 16 Jun 8:30 AM 2008"
Dim format As String = "ddd dd MMM h:mm tt yyyy"
Dim dateTime__1 As DateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture)
hope this may helpful....

Convert string to date in ASP.Net

I'm trying to convert a string into a date format
My string looks like this
Dim MyString as String = "June 2011"
And I'm trying to convert it like this
Convert.ToDateTime(MyString).ToString("MM yyyy")
But it's giving me the error
Syntax error converting datetime from
character string.
The desired output would be either 06/2011 or 01/06/2011. The value of my string will only ever have the month and year in though.
Any ideas?
A datetime object in .NET represents one single point in time. To create one from a Gregorian calendar date, you need all three parts (day, month, year), otherwise it can't represent one single point in time.
Since your business requirement is to assume that the day number is 1 if not provided, just insert that number into the string before parsing.
Dim myDate As Date = Convert.ToDateTime("1 " & MyString)
EDIT:
Sorry, forgot to mention that string manipulations like that are of course culture-dependent. You don't mention what culture you are in. "June 2011" can imply either en-GB or en-US. Since inserting the day number at the start of the string is easier than trying to insert it between the month and year, I suggest you go for something like this.
Dim myDate1 As Date = Date.Parse("1 " & myString, CultureInfo.GetCultureInfo("en-GB"), DateTimeStyles.AllowWhiteSpaces)
Convert.ToDateTime() only takes the full date so try prepending 01 as the day first i.e.
Convert.ToDateTime("01 " + MyString)
DateTime.Parse(MyString).ToString("MM yyyy")
This worked for me
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim MyString As String
Dim d As DateTime = DateTime.Now 'test start
'test all Month Year strings
For x As Integer = 1 To 12
MyString = d.ToString("MMMM yyyy") 'convert date to March 2011 then April 2011 then...
Dim dt As DateTime = DateTime.ParseExact(MyString, "MMMM yyyy", provider)
Debug.WriteLine(MyString & " " & dt.ToString)
d = d.AddMonths(1)
Next

Resources