DateTime format VB.NET - vb.net

I have a text file, which i had extracted the date and time. I need it to be this way
yyyy MM dd HH mm
how can i make it into this format.
I tried
dim date as DateTime = line.Substring(line.Length - 19, 16)
date= DateTime.ParseExact(date, "yyyy MM dd HH mm", CultureInfo.InvariantCulture)
it gave an error

From MSDN, ParseExact has 3 parameters:
s
Type: System.String
A string that contains a date and time to convert.
format
Type: System.String
A format specifier that defines the required format of s.
For more information, see the Remarks section.
provider
Type: System.IFormatProvider
An object that supplies culture-specific format information about s.
So it needs to be something like this instead:
Dim [date] as DateTime =DateTime.ParseExact(line.Substring(line.Length - 19, 16),
"yyyy MM dd HH mm", CultureInfo.InvariantCulture)
Notice that date here will be reported as a non-valid identifier, so you need square braces. A better solution would be to give a meaningful name to your variable. Unfortunately, I cannot suggest one, because you did not provide any context in your question.

Related

Convert ISO time Format To local datetime in vb.net

I have issue to convert ISO date time to local datetime.
the flowing code is not work with me and give error
Dim txt As String = "20200530T015253+08"
Dim output As DateTime = DateTime.ParseExact(txt, "u", System.Globalization.CultureInfo.InvariantCulture)
the error is
System.FormatException: 'String was not recognized as a valid DateTime.'
Your date string is not an "ISO date" as far as I can see. ISO 8601 would lay it out like "2020-05-30T01:52:53+08:00" and .net would parse it with "o"
Parse it by specifying the format:
DateTime.ParseExact("20200530T015253+08", "yyyyMMddTHHmmsszz", Nothing)
If your input data will vary its presentation for time zones that have a fractional hours component such as India being 5.5 hours, then you'll need to vary your format string to zzz

Date format is not getting displayed

I have referred to solutions provided for date in yyyy-MM-dd format. After providing the format also, I am getting date in default format. Please help.
code:
Dim returndate As String
returndate = DateTime.Now.ToString("yyyy-MM-dd")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "yyyy-MM-dd", CultureInfo.InvariantCulture)
As it stands I'm guessing your output of oDate is 27/01/2017 00:00:00.
What you want to do is this:
Dim returndate As String = DateTime.Now.ToString("dd/MM/yyyy")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Debug.Print(oDate.ToString("yyyy-MM-dd"))
Send the returndate into DateTime.ParseExact with the format of "dd/MM/yyyy" and then format oDate to the desired output, in your case "yyyy-MM-dd".
This is a screenshot of the code and output:
Dates don't inherently have a format. The format you pass to ParseExact is the format it expects of the first parameter, in your case returndate. It's not the output format. Have a look over the DateTime.ParseExact documentation:
format
A format specifier that defines the required format of s. For more information, see the Remarks section.
Looking at the Remarks section:
The DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method parses the string representation of a date, which must be in a format defined by the format parameter. It also requires that the date and time elements in s appear in the order specified by format. If s does not match the pattern of the format parameter, with any variations defined by the style parameter, the method throws a FormatException.

Using DateTime.ParseExact throws format exception

I know the question is really asked often, but I can't find the solution to my problem. My code is
Public Function ConvertFacebookDateToNETDate(Instant As String, Format As String) As Date
'Dim UTCOffset As New Integer
'UTCOffset = Instant.Substring(Instant.IndexOf("UTC+") + 4, 2)
Dim MyDateTime As DateTime
MyDateTime = New DateTime()
MyDateTime = DateTime.ParseExact(Instant, Format, CultureInfo.InvariantCulture)
'MyDateTime = MyDateTime.AddHours(-1 * UTCOffset)
Return MyDateTime
End Function
ConvertFacebookDateToNETDate("Friday, May 9, 2014 at 9:48am UTC+02", "dddd, MMMM d, yyyy at h:mtt UTCK")
What is going wrong here ?
Thanks
Two things (all the info comes from there):
In your format you put "at" but "t" if a format specifier so you have to escape it "a\t" or put it between literal delimiter "'at'"
The "K" format specifier for a DateTime with Kind local (a "±XX") needs to be "±XX:XX" so you have to either pass a Date string with that pattern or use "zz" format specifier instead of "K"
There are two problems with the data format
The t in at is interpreted as the short form of tt, i.e. the first character of AM/PM designator. You need to escape it to specify that it's a literal character.
The specifier for time zone offset in hours is z, not K.
So:
ConvertFacebookDateToNETDate("Friday, May 9, 2014 at 9:48am UTC+02", "dddd, MMMM d, yyyy' at 'h:mtt UTCz")

vb.NET (for ASPX page): Parse a string to a date

I'm trying to make this line work:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "yyyy-mm-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
I'm getting date/time strings from spreadsheets in the above format, and I can't control that. There's a ton of help online, including this site, about converting strings to dates, and I've tried them all, but I keep getting this error :
"System.FormatException: String was not recognized as a valid DateTime."
I"m just about ready to write my own custom parser, but that doesn't seem very elegant. Is there some built-in way to convert a string like mine into the date/time format I need?
Thanks for any help.
Your format string is wrong. You're entering a date in d/M/yyyy hh:mm:ss tt format, but telling it to expect a date in yyyy-mm-dd hh:mm:ss format. The two do not match exactly, so DateTime.ParseExact is quite rightly throwing an exception at you.
Try:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "d/M/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
This tells it to expect the following characters:
Integer from 1 through 31 (depending on the length of the month)
/ character
Integer from 1 through 12
/ character
4 digit year
Space
Integer from 1 through 12
: character
Integer from 00 through 59
: character
Integer from 00 through 59
Space
Two character meridian specifier ("AM" or "PM")
For more info on the datetime format strings, check out this MSDN page
I think you need to change the string format to match what you are passing in. You seem to be passing in something more like this:
"d/M/yyyy hh:mm:ss"
Give that a try and see how it works. Note that you need to use MM for months--'mm' is used for minutes.

How translate string to date in .NET

I followed suggestion from vb.net convert string to date . But, it did not work.
the code is as follows:
Dim Dt As DateTime
If DateTime.TryParse("Thu, 09 Dec 2010 16:03:24 EST", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
Can anyone solve this for me? I need to have date populated in the format of "yyyy-mm-dd hh24-mi-ss".
this should get you on the right path:
Dim dt As DateTime
dt = Now
TextBox1.Text = Format(dt, "yyyy-MM-dd HH:mm:ss")
NOTE: this answer was written for a previous revision of the question which had the following code:
Dim Dt As DateTime
If DateTime.TryParseExact("Thu, 09 Dec 2010 16:03:24 EST", "dd.MM.yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
The format string that TryParseExact takes as it's second parameter specifies the format of the date in the string passed as the first.
In your case the format string is specifying that the date will be of the format "09.12.2010" for example - just the day, moth and year. However, as the string isn't in that format it won't parse. If you'd just used ParseExact it would have raised an exception.
The MSDN page for the variant of TryParseExact that takes an array of possible format strings has more examples, but non match your format exactly, but working with the format strings used to convert DateTime to string you probably want something like this:
"ddd, dd MMM yyyy HH:mm:ss ???"
but I can't find what you'd need instead of "???" to match the "time zone as string". You might have to do some string manipulation to remove this before calling TryParse or TryParseExact.
You will have to replace the timezone with the timezone offset.
Same question as Parse DateTime with time zone of form PST/CEST/UTC/etc
TextBox1.Text = System.DateTime.Now.AddMinutes(698).ToString("dd/MM/yyyy");
DateTime DOB;
string[] formatsDOB = { "dd/MM/yyyy", "MM/dd/yyyy" };
DateTime.TryParseExact(txtDateofBirth.Text, formatsDOB, CultureInfo.CurrentCulture, DateTimeStyles.None, out DOB);