0

I have a 2 line code but the date comes out as the following

15/January/2022

but I need it as either

15.01.2022
15012022

Is there a way of doing that?

FirstDay=DateSerial(Year(Date),(Month(Date)- 1),1)
LastDay=DateSerial(Year(Date),Month(Date),0)
user692942
  • 16,398
  • 7
  • 76
  • 175
Vbs.ak
  • 1
  • 1
  • Hi. Thank you for the answer. But i need two liners for two different sap paths. So first one first date of the previous month and the second last day of the previous month. Basically 01.01.2022 and 31.01.2022 – Vbs.ak Feb 03 '22 at 16:24
  • Sorry I tried but it didnt work :S – Vbs.ak Feb 03 '22 at 19:59
  • Waiting a help guys. I need a two seperated code that provide me first day and second day of previous month in the format dd.mm.yyyy. Thanks in advance. – Vbs.ak Feb 03 '22 at 20:54
  • Sorry was rushing the values in [`DateAdd()` function](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/cb7z8yf9(v=vs.84)) were wrong way around. – user692942 Feb 03 '22 at 20:55
  • The day part needs to use `DateAdd()` to increase the date by 1 month then subtract 1 day, this will give you the last day of the month. I’m not building it for you, you have the resources there to have a go yourself. – user692942 Feb 03 '22 at 20:57
  • Thanks, sure i am on it to create. Basically i am rookie. I have 2 questions regarding the string you wrote. What does “00” and “2” stands for Month? FirstDay = "01." & Right("00" & Month(Date()), 2) & "." & Year(Date()) – Vbs.ak Feb 03 '22 at 23:54
  • The [`Right()` function](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/eh8fefz1(v=vs.84)) takes two characters from the end of a string so by prefixing the date part with `"00"` we can guarantee that any single digit values for day or month are padded with a `0`, so the first day of the month becomes `01` not `1`. That is explained in the duplicate target that’s been flagged. – user692942 Feb 04 '22 at 09:29
  • Here's the one liner you need - `lastday = Right("00" & Day(DateAdd("d", -1, DateAdd("m", 1, DateSerial(Year(Date()), Month(Date()), 1)))), 2) & "." & Right("00" & Month(Date()), 2) & "." & Year(Date())`. Tested it and it's works. – user692942 Feb 04 '22 at 09:42
  • 1
    Thank you so much. It was so helpful. I added one extra thing which is Month(Date())-1 since I need previous month's last date. – Vbs.ak Feb 04 '22 at 12:39

0 Answers0