converting string date to dd.MM.yyyy format - vb.net

I am working on vb.net application
am getting date like this :
recevdate = rs("ITIReceiveddate")
my recevdate format is like this : 2/27/2016 month/date/year
i want to convert like this : date.month.year 27.2.2016
so i wrote code like this :
Dim dt as string = DateTime.ParseExact(recevdate, "dd.MM.yyyy", Nothing)
but its getting error ..
What is wrong with my code? how i can rectify this issue?
any help is very appreciable..Thanks

DateTime.ParseExact returns a DateTime, not a string. Your project is setup with the Option Strict set to Off and this enables this kind of automatic conversions. But it is, as usual, a trap waiting to kick on unsuspecting programmers.
To execute correctly you need
Dim recevdate = "2/27/2016"
Dim dt As DateTIme = DateTime.ParseExact(recevdate, "M/d/yyyy", Nothing)
Dim formattedString = dt.ToString("d.M.yyyy")
Console.WriteLine(formattedString)
Notice that you have an error also in your formatted mask for parsing the date. If your date has only one digit for months or one digit for days then you need just one M and one d both on the parsing and in the formatting back to string

Related

Problems converting string to DateTime

I am making a program in which there is a function that check the database for user that haven't been called for 2 weeks or more, and shows them in a ListView.
In this part I am checking how long ago they were called:
Dim r As Int32 = excelWS.UsedRange.Rows.Count
Dim bel As New ArrayList
For nm As Int32 = 1 To r Step 1
If Convert.ToInt32(DateDiff("ww", Date.ParseExact(excelWS.Cells(nm, 1).value(), "yyMMddhhmm", System.Globalization.DateTimeFormatInfo.InvariantInfo), Now, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)) >= My.Settings.Tijdverschil Then
bel.Add(nm)
End If
Next
I get the FormatException was unhandled at the if line.
In the error description it says (roughly translated to english):
The tokens aren't recognized at valid DateTime.
--edit--
If anyone thinks the format in excel is wrong, i copied one field over, they are all like this.
1408180736
Without additional information, I wonder if your date from Excel is coming in as an OLE Automation Date. Depending on how you read the data from Excel, it may come back in this format.
If it is, you need to parse it as a double and then as an OLEDate. Something like this:
Dim oleDate as Double
Dim result as DateTime
If Double.TryParse(excelWS.Cells(nm, 1).value(), oleDate) Then
' Handle valid OLE Automation dates...
result = DateTime.FromOADate(oleDate)
End If
The trick for this is include a datetime picker in your application set visibility to false.
Then set the string as value to the datetime picker and make use of the datetime picker to get the difference between dates.

string to Date Conversion Vb.Net

Very stupid thing but need to achieve in VB.net. I was trying to do this in VB.Net VS2008 .Net Framework 3.5
Dim str As String = "25/05/2014"
Dim dt As Date
dt = DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Console.WriteLine(dt)
And I am getting output as
25-05-2014 00:00:00
Any idea?
This has nothing to do with the conversion, which is OK. When the date is written to the console it is formatted (i.e. converted to a string) with a default format that happens to include the time part.
Specify the desired format when printing:
Console.WriteLine("{0:dd/MM/yyyy}", dt)
or
Console.WriteLine(dt.ToString("dd/MM/yyyy"))
Note: the Date structure (which is the same as the DateTime structure) stores the date and time in a format neutral way, i.e. you cannot format the Date structure. However, when you output it to the user, the date will have to be converted into human readable form, i.e., it will have to be converted to a string. Whenever you convert some binary data (numeric, date/time and so on) into a string, a format will determine how the result will look.
If you're in a hurry, this is a simple way to get the data you want:
Dim sFormatted As String = dt.Day.ToString() & "/" & dt.Month.ToString() & "/" & dt.Year.ToString()
MessageBox.Show(sFormatted)

Working with Excel Dates in vb.net

I am experiencing a problem when attempting to read a date from an excel sheet. (The date column is formatted the same as the short date format of the computer its opened on). I populate the dates from the excel sheet into a datagrid with success, but when I attempt to parse the date (To format it appropriately), I get a error saying the string wasn't a valid DateTime value. The computer's short date format is dd/MM/yyyy. This is the code I use to try parsing the date. The following code is an example of where the process fails.
Dim dateParsed AS DateTime = DateTime.Parse("14/01/2013").ToString("yyyy-MM-dd")
Is there some way to programatically get the system's short date format and use ParseExact instead or any suggestions?
You want to parse exact, using the current cultures short-date format?
Dim dt As DateTime = DateTime.ParseExact(str, "d", CultureInfo.CurrentCulture)
Additionally, if you actually need to see the ShortDate format for yourself get it through the CurrentCulture.
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
You can try this piece of coding
Dim dt As DateTime = DateTime.Parse("1/14/2013").ToString
Dim f As String = Format(dt, "yyyy-MM-dd")

Convert string to datetime in vb.net

I have a datetime that looks like this:
201210120956
ccyyMMDDhhmm
When I try this:
Dim convertedDate As Date = Date.Parse(DateString)
Return convertedDate
I get back this:
#10/12/2012#
I'm losing the time on it.
I've read this convert string to datetime vb.net but when I use datetime.ParseExact() I get:
cannot resolve symbol 'ParseExact'
Is there a way to convert this to a date time without using substring? A straight conversion?
Pass the decode pattern to ParseExact
Dim d as string = "201210120956"
Dim dt = DateTime.ParseExact(d, "yyyyMMddhhmm", Nothing)
ParseExact is available only from Net FrameWork 2.0.
If you are still on 1.1 you could use Parse, but you need to provide the IFormatProvider adequate to your string
You can try with ParseExact method
Sample
Dim format As String
format = "d"
Dim provider As CultureInfo = CultureInfo.InvariantCulture
result = Date.ParseExact(DateString, format, provider)
As an alternative, if you put a space between the date and time, DateTime.Parse will recognize the format for you. That's about as simple as you can get it. (If ParseExact was still not being recognized)

Yet another date formatting problem :(

I seem to have a date formatting problem every day!
I am querying a table and am getting a date back in the format dd/mm/yyyy (as a string btw). Brilliant! thats what I want. But, now I want to convert that string to a date so i can do
dim dayNumber as integer = day.DayOfWeek
But when I convert it to a date it changes it to #m/dd/yyyy#. AHHHH! how can I change this?
here is my code i've tried
Dim ActivityDate As String
If dt.Rows(i)("Date") Is DBNull.Value Then
ActivityDate = ""
Else
ActivityDate = dt.Rows(i)("Date")
End If
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse(ActivityDate, ci)
Dim day As Integer = theDate.DayOfWeek
Cheers
Brilliant! thats what I want
That's not what you want. It is the worst possible format for a date because it is so horribly ambiguous. Date string formats depend on the current culture. "4/1/2010" is Unicorn day at SO, it is day in January in Europe. "#4/1/2010#" is a legacy VB6 format.
Always store dates in a DateTime in your code. Always store dates in a database column type of datetime in your dbase. There is never any ambiguity and you'll have an easy time with the DateTime members to manipulate dates.
If you convert the string to a date, you can always output it back to the original format using a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The correct solution here (at least until you tell us why this isn't possible) is to update your database to use a datetime column type rather than a varchar. Now we also know that this column has no NULL values, because otherwise you'd be complaining about exceptions on your Date.Parse() call. After applying both those sentences, you can trim all that code down to a simple one-liner:
Dim day As Integer = DirectCast(dt.Rows(i)("Date"), DateTime).DayOfWeek
May I also ask why you're looping through the table row by row? I've worked in a shop where that was the norm, but since I've left there I've run in to alternatives and more and more I'm coming to find looping through a datatable as just wrong. It's an older imperative coding style, and generally you want to go for a declarative coding style.
Are you parsing it like this:
Dim newDate as DateTime = DateTime.Parse(myDate)
If the culture of your system does not use that date format, then you should get that date string as an actual date:
' canadian date format is dd/mm/yyyy
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse("13/04/2010", ci)
Make sure you specify an exact parse format like so:
Console.WriteLine(DateTime.ParseExact("17/12/2010", "dd/mm/yyyy", null));
I am not sure what the last parameter is but it is safe to ignore it.
I'm guessing that you are seeing the #m/dd/yyyy# in the debugger, like this screenshot below. Don't worry!
A Date variable isn't stored as a string. The debugger has to convert your Date into a string to display it, and it insists on showing dates in #m/dd/yyyy# format. But that doesn't have any effect on the runtime behaviour of your program.
Screenshot of Visual Studio Debugger http://img707.imageshack.us/img707/6205/debugger.gif