I am getting this datetime format in an xml file:
2012-06-14T11:15:41.587-07:00
2012-06-14T10:49:32.397-07:00
2012-06-11T11:10:49.2-07:00
I believe I understand some of it, please correct me if I am wrong:
2012-06-14 = date
T = time identifier
10:49:32.397 = hour min second milliseconds
-07:00 = I have no idea
I need to convert this to something I can sort on for my datagrid view.
When I try something like this:
Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", "2012-06-14T10:49:32.397-07:00"))
I'm getting the original string back out with no conversion.
Anyone have any suggestions?
-07:00 = I have no idea
-7 is a timezone offset. It means the DateTime is 7 hours behind UTC, which would indicate the US Mountain Time.
You want to use DateTime.Parse to get a DateTime object.
Dim val As String = "2012-06-11T11:10:49.2-07:00"
Dim parsedDateTime As DateTime = DateTime.Parse(val)
'Do whatever with parsedDateTime here
Take a look at this article:
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
It should give you a better grasp of how to use the DateTime object. The time you have is the ISO 8601 format.
The -07:00 is the offset from UTC. You can parse this via DateTimeOffset.ParseExact, which includes a specifier for the offset ("zzz" format specifier).
In your case, I believe this would be :
Dim xmlValue = "2012-06-14T11:15:41.587-07:00"
Dim value as DateTimeOffset = DateTimeOffset.ParseExact(xmlValue, "yyyy-MM-dd\Thh:mm:ss.fffzzz", CultureInfo.InvariantCulture)
Related
How to compare two datetime values?
For example as per below condition, I have FDatetimes value is string coming (i.e 2021-06-21 17:00:30Z) from database and SDatetimes value should be Datetime.UtcNow. So my requirement is both left and right side always compare with datetime instead of string.
Also I have tried to use Convert.ToDateTime(FDatetimes). But there is one issue here. Whenever I have used the above function, That time the time is getting changed to 2021-06-21 10:00:30Z. My main aim is to compare both field value as DateTime instead of String. Please help in VB.Net
If FDatetimes < SDatetimes Then
'My logic
End If
So to resolve DST issues I did this (which I ran at 9:20AM local, 14:20 UTC on 23 Jun 2021)
Dim SDatetimes As DateTime = DateTime.UtcNow
Dim FDatetimes As String = "2021-06-23 15:20:30Z"
Dim FDT As DateTime = DateTime.ParseExact(FDatetimes,
"yyyy-MM-dd HH:mm:ssZ",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.AdjustToUniversal Or
Globalization.DateTimeStyles.AssumeUniversal)
Debug.WriteLine(SDatetimes.ToString)
Debug.WriteLine(FDT.ToString)
If FDT < SDatetimes Then
'My logic
Stop
End If
I have below code to get StringToDate as 04/29/2019, but when use CDate function it's giving me #4/29/2019 12:00:00 AM# value. which date format i have to use to get 04/29/2019
Dim StringToDate As Date
Dim strNewDate As String = "04/29/2019"
StringToDate = CDate(strNewDate)
You could use DateTime.TryParse
Dim MyDate As DateTime = Nothing
Dim strNewDate As String = "04/29/2019"
DateTime.TryParse(strNewDate, MyDate)
Edit (misread your question)-
A DateTime holds value and not String. To get what you asking after parsing the date you would need to convert it back to a string with a format method or use the following:
MyDate.ToShortDateString
The way a date is represented is by what the region settings of your OS have setup.
For example, if you were to open the region settings in Control Panel you would find a format for Short Date which by what you're describing is probably M/d/yyyy. Inside the dropdown are other formats you can set and if you were to set it to MM/dd/yyyy you would see that your same code would now display 04/29/2019. But Don't Do This as it only affects your machine. Instead, understand that a date is only number, and you can control how it is displayed, the same way Windows just did from my example. A perfect example of this is that you could set a DateTime variable in many different ways. Here's a few:
Dim dt1 As DateTime = #2019-12-14#
Dim dt2 As DateTime = #1/08/2019#
Dim dt3 As DateTime = #03/03/2019#
Dim dt4 As DateTime = #4/4/2019#
The results would be
12/14/2019 12:00:00 AM
1/8/2019 12:00:00 AM
3/3/2019 12:00:00 AM
4/4/2019 12:00:00 AM
because that is how windows is set up to show me a DateTime as a string.
When you want to display this date to the user in a different format you can simply supply a format to the .ToString() method or use one of the DateTime string conversion methods (ie, .ToShortDateString, .ToLongDateString, etc) You can show the date to the user in a more preferable format.
Taking the last example (dt4) you could use
Console.WriteLine(dt4.ToShortDateString) 'and show 4/4/2019
Console.WriteLine(dt4.ToLongDateString) 'and show Thursday, April 4, 2019
Console.WriteLine(dt4.ToString("MM/dd/yyyy")) 'and show 04/04/2019
So I have this datetime value of 9.3.2016 18:56:12, by using datetime.parse, I can get the values but instead of getting '3' as the month, it takes '9' as month and '3' as day which is incorrect.
dim d1 as string = "9.3.2016 18:56:12"
dim d2 as datetime = datetime.parse(d1, CultureInfo.InvariantCulture)
I don't want to use datetime.parseExact because I'm having more than 1 value in the database.
Please help :( thank you!
Since you current culture appears to interpret dates in the way you expect you could simply do this,
Dim dateString = "9.3.2016 18:56:12"
Dim dateValue = DateTime.Parse(dateString)
By not specifying the InvariantCulture, you instruct DateTime.Parse to use the current culture which, in your case, interprets the date string correctly.
Sorry I've got the solution.
dim d2 as datetime = convert.todatetime(d1)
I have a date in string variable strOrderEndDate which looks like this 8/14/2015.
I would like to convert it to 2015-08-14.
How do I do this in vb.net?
I tried strOrderEndDate.ToString(yyyy mmmm dd) but its not working
As the comments indicate, you first need to convert the string to a date using CDate. Then you can use the .ToString method on the new date type variable to format the output as desired:
Dim strOrderEndDate As String = "8/14/2015"
Dim datOrderEndDate As Date = CDate(strOrderEndDate)
MsgBox(datOrderEndDate.ToString("yyyy-MM-dd"))
...or as Plutonix recommends, you can use DateTime.Parse to perform the conversion. He's pretty smart so if he says it's better, then it's better.
Dim datOrderEndDate As Date = DateTime.Parse(strOrderEndDate)
i have a string which has a value of "08-06-2008". I want the result to look like "08-06-08". Is there a way to do this? I've tried CDate but that only gives me 8/06/2008 which doesnt resolve the issue.
Parse it to Date and back to string:
Dim dt As Date = Date.ParseExact("08-06-2008", "MM-dd-yyyy", CultureInfo.InvariantCulture)
Dim result As String = dt.ToString("MM-dd-yy", CultureInfo.InvariantCulture)
Since that is a normal format you could also omit the format string and use Date.Parse directly:
Dim dt As Date = Date.Parse("08-06-2008", CultureInfo.InvariantCulture)
I have used CultureInfo.InvariantCulture to avoid localization issues, normally your current culture is used in Parse/ParseExact and ToString.
See: Custom Date and Time Format Strings
Firstly, avoid CDate. It is a hangover from VB6, and is almost certainly less efficient than using the .net equivalent.
The following should give you the right answer:
string value = DateTime.ParseExact("08-06-2008", "dd-MM-yyyy").ToString("dd-MM-yy")
Note that your existing date format is ambiguous, so I've been unable to tell if you're meaning the 6th of August or the 8th of June - this is why I've used ParseExact over Parse. ParseExact is also slightly more efficient, as it tells the framework which format to use, rather than it having to guess.
Try this
Dim FormatString As String = ""
Dim SampleDate As DateTime
SampleDate = Now()
FormatString = Format(SampleDate,"dd-MM-yy")