Getting the right date....

  • Thread starter Thread starter Drygast
  • Start date Start date
D

Drygast

I have a form where I would like to get the todays date and when the date is
15 or less I would like to assign a textfield the last date of previous
month
and when the date is larger then 15 I need to set the textfield to the 15:th
of present month....

The code is wrong I know, but to clearify a little how I'm thinking:

If Date.Now <= 15 Then

txtTo.Text = lastday(Date.Now-1month)

Else

txtTo.Text = date.15

End If



Regards

/Drygast
 
Drygast said:
I have a form where I would like to get the todays date and when the
date is 15 or less I would like to assign a textfield the last date
of previous month
and when the date is larger then 15 I need to set the textfield to
the 15:th of present month....

The code is wrong I know, but to clearify a little how I'm
thinking:

If Date.Now <= 15 Then

txtTo.Text = lastday(Date.Now-1month)

Else

txtTo.Text = date.15

End If

Dim d As Date = Date.Today

If d.Day <= 15 Then
d = d.AddDays(-d.Day)
Else
d = d.AddDays(15 - d.Day)
End If

txtTo.Text = d.ToString
 
probably not the cleanest sollution but it works

Dim strtest As String

Dim strresult As String

strtest = "15/" & Now.Month & "/" & Now.Year

If Now > strtest Then

strresult = "15/" & Now.Month & "/" & Now.Year

Else

Dim intm, intj As Int16

intj = Now.Year

intm = Now.Month - 1

If intm = 0 Then

intm = 12

intj -= 1

End If

strresult = "15/" & intm & "/" & intj

MessageBox.Show(strresult, "test")

End If
 
Hi Drygast
I have a form where I would like to get the todays date and when the date is
15 or less I would like to assign a textfield the last date of previous
month
and when the date is larger then 15 I need to set the textfield to the 15:th
of present month....
The code is wrong I know, but to clearify a little how I'm thinking:
If Date.Now <= 15 Then
txtTo.Text = lastday(Date.Now-1month)
Else
txtTo.Text = date.15
End If

Does this looks like it?

\\\
Dim a As System.DateTime
If Now.Day < 15 Then
a = New System.DateTime(Now.Year, Now.Month, 15)
Else
If Now.Month = 1 Then
a = New System.DateTime(Now.Year - 1, 12, 15)
Else
a = New System.DateTime(Now.Year, Now.Month - 1, 15)
End If
End If
MessageBox.Show(a.ToString)
///
Cor
 
Thanks EricJ, Armin and Cor
you've all suggested similar solutions which work perfectly! Thanks!

But I also need to get the last date of previous month (doesn't work just
setting the date to 31)

recap:
If the date is < 15 then I need to get the last date (highest possible date
for instance 28 for febuary) in the previous month.
 
Hi Drygast,
I made that first, and then I thought you wanted something else,
This is something the same as Armin made the rest you have to do yourself.
(Did not check the code on typing errors I changed it at the last moment)
\\\
Dim a As String
If now.day > 15 Then
a = (now.day - (System.DateTime.DaysInMonth(Now.Year, Now.Month))).ToString
Else
a = now.day.ToString
End If
MessageBox.Show(a)
///
Cor
 
If Date.Now <= 15 Then
txtTo.Text = dateserial(date.now.year, date.now.month, 0)
Else

txtTo.Text = date.15

End If
 
the 15th of present date is:

dateserial(date.now.year, date.now.month+1,15)



hope this helps

re, habix
 
Back
Top