Parsing a Date.Now using vb.net

cmdolcet
cmdolcet used Ask the Experts™
on
I am trying to parse a return value of the date.now

I have the following:

'On a button control I have the following

Dim date1 as Date = Date.now

'I would like to parse the returned value # 9/5/2019 01:27:04 PM#

Open in new window


The format I am trying to get is the following
<min,hour,date,weekday,month,year>

So the returned value above would look like:

<27,01,05,4,9,2019>

Any help would be amazing.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jorge SanchezSoftware Engineer

Commented:
This should work:

Date.Now.ToString("<mm,HH,dd,") & cint(Date.Now.DayOfWeek) & Date.Now.ToString(",MM,yyyy>")

Open in new window

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
In .Net we have DateTime data type and everything that you need is available as a property. So, date1.Hour will get you Hours value and so on. All you need to do is use concatenation to get desired results.

date1.Hour &  "," & date1.Minute...

https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8#properties
Senior .Net Consultant
Top Expert 2016
Commented:
The missing part in the ToString is DayOfWeek. So you need to do a bit of gymnastic:
            dim d as DateTime = DateTime.Now
            Dim s as String = d.ToString("mm,hh,dd,@w@,M,yyyy").Replace("@w@", Convert.ToInt32(d.DayOfWeek).ToString)

Open in new window


More details at https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Commented:
*NO POINTS*

As Eric states:
Imports System

Module Program
	Sub Main(args As String())
		Dim [date] As DateTime = DateTime.Now
		Console.WriteLine([date])
		Console.WriteLine($"<{[date].ToString("mm,hh,dd,@w@,MM,yyyy")}>".Replace("@w@", Convert.ToInt32([date].DayOfWeek)))
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.PNG-saige-
Here is a function that you could use:
 Function Adjustdate(ByVal i As DateTime) As String
        ' <min,hour,day,weekday,month,year>
        Return "<" & i.Minute & "," & i.Hour & "," & i.Day & "," & i.DayOfWeek & "," & i.Month & "," & i.Year & ">"
    End Function

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial