Question Changing the date display format

teymoorei

Active member
Joined
Jan 11, 2012
Messages
36
Programming Experience
1-3
Hello friends

I use the following function to get the date:


VB.NET:
 Public Function Shamsi_Date() As String
        Dim DT As Date = Date.Now
        Dim Glob As New Globalization.PersianCalendar
        Shamsi_Date = Glob.GetYear(DT) & "/" & Glob.GetMonth(DT) & "/" & Glob.GetDayOfMonth(DT)
    End Function

Now I have two questions

1- I want the date to be displayed in the form (01/12/1401), not like this (1/12/1401), that is, if the day or month is a single digit, put 0.

i liked ==> (yyyy/mm/dd) no (yyyy/m/d)

<unrelated question removed by moderator>

Thanks
 
Last edited by a moderator:
Replace this:
VB.NET:
Shamsi_Date = Glob.GetYear(DT) & "/" & Glob.GetMonth(DT) & "/" & Glob.GetDayOfMonth(DT)
with this:
VB.NET:
Return $"{Glob.GetYear(DT):0000}/{Glob.GetMonth(DT):00}/{Glob.GetDayOfMonth(DT):00}"
That uses string interpolation, which is basically native language support for the String.Format method. To understand how composite formatting works, read the documentation for that method and follow the appropriate link(s).
 
The VS.NET General forum is for general questions about the VS IDE, not for coding questions. Please post in the most appropriate forum for the topic, not the first one in the list. Thread moved.
 
The second problem:
Message me two days before the desired date
I didn't even notice that. That is completely unrelated to the first question and so it belongs in a separate thread. I will delete that from the OP and you should create a new thread for the new topic, providing a much more detailed explanation of the situation and the requirement.
 
I think my question is related to this topic
Because I have a date function whose codes are available in this thread.
I just want to get the "two days before" arrival date obtained by the above function with this function
 
I think my question is related to this topic
It's not. Converting a Date to a String is completely unrelated to displaying a notification based on a Date. Also, calculating a Date based on another Date are different things to. You asked for a notification originally but now you seem to be suggesting that you just want to calculate when to display that notification. This is an example of why you need to create a dedicated thread and provide a FULL and CLEAR explanation of the problem. We only know what you tell us so if you tell us two different things then we don't know what you want.
 
The problem was solved
This is a sample code :

VB.NET:
 Private Function ShamsiDateDiff(ByVal Date1 As String, ByVal Date2 As String, Optional ByVal InterVal As DateInterval = DateInterval.Day) As Integer

        Dim MDate1, MDate2 As Date

        Dim SDate1 = Split(Date1, "/")
        Dim SDate2 = Split(Date2, "/")

        Dim Pc As New Globalization.PersianCalendar

        MDate1 = Pc.ToDateTime(SDate1(0), SDate1(1), SDate1(2), 0, 0, 0, 0)
        MDate2 = Pc.ToDateTime(SDate2(0), SDate2(1), SDate2(2), 0, 0, 0, 0)

        Return DateDiff(InterVal, MDate1, MDate2)

    End Function

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        MsgBox(ShamsiDateDiff(txtDate1.Text.Trim, txtDate2.Text.Trim, DateInterval.Day))
    End Sub
 

Latest posts

Back
Top