Reputation: 63
How to get previous month name and year using format() or any way possible in vb script?
Ex: if current month = february, i want output = January2014 if current month = january 2014, i want output = December 2013
Kindly help.
Upvotes: 1
Views: 12416
Reputation: 16311
All of the previous answers were for VB.NET. If you want a VBScript solution, try this:
' Here's how you can get the starting and ending days of the previous month...
dtEnding = Date - Day(Date)
dtStarting = dtEnding - Day(dtEnding) + 1
' To display the month and year...
MsgBox MonthName(Month(dtEnding)) & " " & Year(dtEnding)
Upvotes: 5
Reputation: 7500
In VB.Net you can use .AddMonth(-1):
Dim newDate as DateTime = DateTime.Now.AddMonths(-1)
use .ToString() with a format supplier to return it in the correct format:
Console.WriteLine(newDate.ToString("MMMM yyyy"))
In VBScript you can use the DateAdd function:
Dim newDate : newDate = DateAdd("m", -1, now)
And use a stringbuilder to format it right
wscript.echo CreateObject("System.Text.StringBuilder").AppendFormat("{0:MMMM yyyy}", newDate).ToString()
Upvotes: 1
Reputation: 1613
Try this example.
Dim dateGiven As DateTime = Date.Now
dateGiven = dateGiven.AddMonths(-1)
MsgBox(dateGiven.ToString("Y"))
if you want to remove the comma, do this
Dim dateGiven As DateTime = Date.Now
Dim output As String = ""
dateGiven = dateGiven.AddMonths(-1)
output = dateGiven.ToString("Y")
output = output.Replace(",", "")
MsgBox(output)
Upvotes: 1