Convert ISO time Format To local datetime in vb.net - 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

Related

Compare code date to sql date ASP NET

I get the error about convert type date when i try to compare this :
Page Load :
Dim DataCorrenteLoad As String = DateTime.Now.ToString("dd-MM-yyyy")
Label2.Text = DataCorrenteLoad
and in SqlDataSource the where condition is that the FinishDate >= Today date
How can i solve this situation ? Should i change the format dd-mm-yyyy ? Or what ?
Thanks.
Seems, you want to compare dates, but you kept your focus on string representation of a date. This is not the same!
Take a look at example:
Dim DatabaseDate As DateTime = DateTime.Today.AddDays(-5)
Dim CurrentDate As DateTime = DateTime.Today
'case 1: compare and display date in current culture - depending on regional settings
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d"), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d"))
'case 2: change string representation of date:
'compare dates in France and German format
Dim cu1 As Globalization.CultureInfo = New Globalization.CultureInfo("Fr-fr")
Dim cu2 As Globalization.CultureInfo = New Globalization.CultureInfo("De-de")
DatabaseDate = DateTime.Today
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d", cu1), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d", cu2))
Above code returns:
'case 1
2017-06-08 is not equal to 2017-06-13
'case 2
13/06/2017 is equal to 13.06.2017
Conclusion:
Depending on OS regional settings, the date format may differ, but the date is still the same!
For further details, i'd strongly recommend to read these:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Parsing Date and Time Strings in .NET
Design and Implementation Guidelines for Web Clients
Formatting Date and Time for a Specific Culture
Label2.Text= System.DateTime.Now.ToString("dd-mm-yyyy");
use this it will work System.DateTime.Now will show current time when you use To strong("dd-mm-yyyy") it will convert it in to string and set it's formate which u set under double quotes.

Convert string to date with format (VB.net)

I have a string with a date in this format. "24-11-2015" or "dd-MM-yyyy". I need to convert this to a date with this format. "2015-11-24" or "yyyy-MM-dd".
I've tried several ways, and all of them rely on my system format for this to work. If i run my program on a computer with english time format this works.
Date.ParseExact("24-11-2015", "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
But if i run it on a computer running danish format i get error because there is no 24th month. How do i make the date format independent of my system time format.
Solution:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Console.WriteLine("{0}-{1}-{2}", dt.Year, dt.Month.ToString("D2"), dt.Day.ToString("D2"))
Solution 2:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Dim format As String = "yyyy-MM-dd"
Dim str As String = dt.ToString(format)
Console.WriteLine(str)
You can also try Formatting Date and Time for a Specific Culture.

not able to convert string date format in vb.net

I'm taking date from my csv file
Dim odateq As String = sData(0).Trim()
I am getting odateq as 9/15/2015
I want to convert this to 15/9/2015. So I wrote code like this
Dim newdate As DateTime = DateTime.ParseExact(odateq, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture)
And I'm getting an error like this :
String was not recognized as a valid DateTime.
any help is very appreciable...thanks
the code you wrote is using the wrong format; the date you have is in the format M/d/yyyy (month and day without leading zero are a guess because you did not specify it).
try with this one:
Dim newdate As DateTime = DateTime.ParseExact(odateq, "M/d/yyyy", System.Globalization.CultureInfo.CurrentCulture)
the format you set in the ParseExact was telling the function to expect a date in the format dd/MM/yyyy like 01/05/2015 but what you have is not in that format.
after you parse the input date, to get a string with format dd/MM/yyyy use:
Dim dateAsText As String = newdate.ToString("dd/MM/yyyy")

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);