Get firstDate previous month & lastDate previous month?

I want get firstDate previous month & lastDate previous month ?

Example Now 12/04/2565

I want to FirstDate as String = 25650301 (Format yyyyDDMM)
and LastDate as string = 25650331 (Format yyyyDDMM)

Please guide me about it

use my sequence for reference
test11.xaml (8.6 KB)

you declare DateTime nowDate and set it to your date
then calculate first date of current month, and from that the last and first date of prev month

1. nowDate = DateTime.ParseExact("12/04/2565", "dd/MM/yyyy",nothing)
2. dFirstDayOfThisMonth = nowDate.AddDays(-(DateTime.Today.Day-1))
3. dLastDayOfLastMonth = dFirstDayOfThisMonth.AddDays (-1)
4. dFirstDayOfLastMonth = dFirstDayOfThisMonth.AddMonths(-1)

result
image

1 Like

Hi @fairymemay ,

Here is an alternate solution →
image

First date of previous month:

DateTime.ParseExact(str_date,"dd/MM/yyyy",Nothing).AddMonths(-1).ToString("yyyyMM01")

Last date of previous month:

DateTime.ParseExact(str_date,"dd/MM/yyyy",Nothing).AddDays(-Cint(str_date.Substring(0,2))).ToString("yyyyMMdd")

FirstndLastDatePrevMonth.xaml (5.3 KB)

Kind Regards,
Ashwin A.K

1 Like

@jack.chan

If I don’t fix date = 12/04/2565 —> check auto ?

DateTime.ParseExact("12/04/2565", "dd/MM/yyyy",nothing)

How to edit it?

if you want auto date do this

nowDate = Now()
image
image

Hi,

I guess 2565 is Buddhist era, right? If so, the following expression will work and can also handle leap year correctly.

strDate = "12/04/2565"
dateVar = DateTime.ParseExact(System.Text.RegularExpressions.Regex.Replace(strDate,"\d{4}$",Function(m) (Int32.Parse(m.Value)-543).ToString()),"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture)

Then

System.Text.RegularExpressions.Regex.Replace(New DateTime(dateVar.Year,dateVar.Month,1).AddMonths(-1).ToString("yyyyMMdd"),"^\d{4}",Function(m) (Int32.Parse(m.Value)+543).ToString())

System.Text.RegularExpressions.Regex.Replace(New DateTime(dateVar.Year,dateVar.Month,1).AddDays(-1).ToString("yyyyMMdd"),"^\d{4}",Function(m) (Int32.Parse(m.Value)+543).ToString())

If you need to use today’s date, please try dateVar = Now

Regards,

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.