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")
Related
is there anyway to convert "20180717" in to datetime "MM dd yyyy" format in vb.net
im reading csv file and pick up values related to date all date values are in like "20180717" format need to store thoses in datatable DateTime column in sql
You can also use DateTime.TryParseExact. This method doesn't throw an Exception in case the input isn't valid:
Dim dtValue As DateTime
If DateTime.TryParseExact("20180717", "yyyyMMdd", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtValue) Then
'The input is a valid date in specified format. The parsed date is now in dtValue
Else
'The input isn't a valid date (in specified format).
End If
DateTime.ParseExact(csvstring,"yyyyMMdd",CultureInfo.InvariantCulture).ToString("MM dd yyyy")
I have user input that needs to be verified that the string is in the datetime format as follows:
yyyy-MM-dd HH:mm:ss
The user does not have a choice for any other format but for error checking how do I detect whether string in is this format.
sDate = DateTime.ParseExact(startDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture)
startdate is the string.
This code will help you to convert the date into the format which which you wanted to check
Dim format As String = "yyy-MM-dd HH:mm:ss" 'let input be "02-02-2014 12:12:12"
MsgBox(dt.ToString(format)) ' the output will be 2014-02-02 12:12:12
it works properly in VS 2010(windows 7)
IMHO, you should consider using DateTimePicker control for taking date/time input. Use Custom Format (if needed). It will save you to first take it and validate it.
You should use DateTime.TryParseExact if you want the date/time format to be exactly in a particular format.
Function IsValidDate(ByVal dateValue As String) As Boolean
Return DateTime.TryParseExact(dateValue, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)
End Function
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.
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.
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);