none
pharmacist enter expiry date of medicine RRS feed

  • Question

  • hi

    Hi,

    how to solve this problem which have task about Date?  Pharmacist  Insert Medicine Name and it Expiry Date in Textbox2.text,  further more in expiry date they insert only month and year,  Day of month end should be taken by system like pharmacist enter 02-2021  it means when cursor leave Textbox2 it become a 28-02-2021.  Keep in mind leap year also


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959


    Friday, November 15, 2019 10:18 AM

Answers

  • Okay here you go

    Public Class Form1
        Private Sub CheckExpiryEntryButton_Click(sender As Object, e As EventArgs) _
            Handles CheckExpiryEntryButton.Click
    
            If Not String.IsNullOrWhiteSpace(ExpiryDateTextBox.Text) AndAlso ExpiryDateTextBox.Text.Contains("-") Then
                Dim index = ExpiryDateTextBox.Text.IndexOf("-")
                ExpiryDateTextBox.Text = ExpiryDateTextBox.Text.Insert(index, "-" & Now.Date.Day.ToString())
                Dim expiry As Date
                If Date.TryParse(ExpiryDateTextBox.Text, expiry) Then
                    Dim today As Date = Date.Today
                    Dim endOfMonth As New DateTime(expiry.Year, expiry.Month, Date.DaysInMonth(expiry.Year, expiry.Month))
                    EndOfMonthTextBox.Text = endOfMonth.ToShortDateString()
                Else
                    MessageBox.Show($"{ExpiryDateTextBox.Text} is not a valid date")
                End If
            Else
                MessageBox.Show("Enter month-year e.g. 02-2021")
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by muhammadanzar Friday, November 15, 2019 3:30 PM
    Friday, November 15, 2019 2:22 PM
    Moderator

All replies

  • See if the following works for you.

    Private Sub CheckExpiryEntryButton_Click(sender As Object, e As EventArgs) _
        Handles CheckExpiryEntryButton.Click
    
        If Not String.IsNullOrWhiteSpace(ExpiryDateTextBox.Text) AndAlso ExpiryDateTextBox.Text.Contains("-") Then
            Dim index = ExpiryDateTextBox.Text.IndexOf("-")
            ExpiryDateTextBox.Text = ExpiryDateTextBox.Text.Insert(index, "-" & Now.Date.Day.ToString())
            Dim expiry As Date
            If Date.TryParse(ExpiryDateTextBox.Text, expiry) Then
                MessageBox.Show(expiry.ToShortDateString())
            Else
                MessageBox.Show($"{ExpiryDateTextBox.Text} is not a valid date")
            End If
        Else
            MessageBox.Show("Enter month-year e.g. 02-2021")
        End If
    
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 15, 2019 12:07 PM
    Moderator
  • Hi,

    You did great work,  but i need month end dates like

    January ->31-mm-yyyy

    February->28-mm-yyyy

    March->31-mm-yyyy

    ..............

    .............

    .........

    December->31-mm-yyyy

    thanks for reply


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Friday, November 15, 2019 1:27 PM
  • See the following which you need to adapt to your code as I'm heading off to work. If you run into issues reply back and I will look at it once at work.

    Dim today As Date = Date.Today
    Dim endOfMonth As New DateTime(today.Year, today.Month, Date.DaysInMonth(today.Year, today.Month))
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 15, 2019 1:48 PM
    Moderator
  • Hi

    Your image seems contrary to what I thought you wanted (last day in given month).

    Anyway, here is one way to get the LastDayOfMonth date formatted as needed.

    This example uses a Form1 with DateTimePicker1, Label1 and Button1

    	Function GetLastDay(d As Date) As Date
    		Dim dd As Integer = Date.DaysInMonth(d.Year, d.Month)
    		Return New Date(d.Year, d.Month, dd)
    	End Function
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		' example of use
    		' format as needed
    		Label1.Text = GetLastDay(DateTimePicker1.Value).ToString("dd-MM-yyyy")
    	End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, November 15, 2019 1:52 PM
    Friday, November 15, 2019 1:50 PM
  • Okay here you go

    Public Class Form1
        Private Sub CheckExpiryEntryButton_Click(sender As Object, e As EventArgs) _
            Handles CheckExpiryEntryButton.Click
    
            If Not String.IsNullOrWhiteSpace(ExpiryDateTextBox.Text) AndAlso ExpiryDateTextBox.Text.Contains("-") Then
                Dim index = ExpiryDateTextBox.Text.IndexOf("-")
                ExpiryDateTextBox.Text = ExpiryDateTextBox.Text.Insert(index, "-" & Now.Date.Day.ToString())
                Dim expiry As Date
                If Date.TryParse(ExpiryDateTextBox.Text, expiry) Then
                    Dim today As Date = Date.Today
                    Dim endOfMonth As New DateTime(expiry.Year, expiry.Month, Date.DaysInMonth(expiry.Year, expiry.Month))
                    EndOfMonthTextBox.Text = endOfMonth.ToShortDateString()
                Else
                    MessageBox.Show($"{ExpiryDateTextBox.Text} is not a valid date")
                End If
            Else
                MessageBox.Show("Enter month-year e.g. 02-2021")
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by muhammadanzar Friday, November 15, 2019 3:30 PM
    Friday, November 15, 2019 2:22 PM
    Moderator