How to Get Previous Month from Selected Month

How to get next month and previous month from String (Month and Year)

Get month,year from Calender as String , then parse it as LocalDate then format it through formater .

Example-

public class MainActivity extends AppCompatActivity {
private TextView monthView;
Button next, previous;
public static LocalDate localDate;
private String dateFormat;


@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

monthView = findViewById(R.id.tvLDate);
next = findViewById(R.id.next);
previous = findViewById(R.id.previous);


getDate();

next.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {

localDate = localDate.plusMonths(1);
monthView.setText(monthFormat(localDate));

}
});
previous.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
localDate = localDate.minusMonths(1);
monthView.setText(monthFormat(localDate));
}
});



}

@RequiresApi(api = Build.VERSION_CODES.O)
private void getDate() {
Calendar calendar = Calendar.getInstance();
String month = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
String year = String.valueOf(calendar.get(Calendar.YEAR));
dateFormat = "01/" + month + "/" + year;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMMM/yyyy");
localDate = LocalDate.parse(dateFormat, formatter);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private String monthFormat(LocalDate date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM");
return date.format(formatter);
}
}

Get previous month value based on month value in a cell in excel

B1 =TEXT(EDATE(--(A1&1),-1),"mmm")

Get the previous month name based on a supplied month name using JavaScript

Just uses an array of month names. Lookup index and -1. || is for if 0 use 12, so January wraps around to December.

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
function getLastMonth(month){return months[(months.indexOf(month) + 12 - 1) % 12]}
console.log( getLastMonth('January'), getLastMonth('November'), getLastMonth('December'))

How to reliably get previous month from js Date?

Just use clone.setDate(0) and you will get the last day of previous month

const dates = [new Date(2021,0,15), new Date(2021,2,31)]

const getPreviousMonth = date => {
const clone = new Date(date)
clone.setDate(0)
return clone
}

dates.forEach(d=>{
console.log(getPreviousMonth(d))
})

Get the previous month number including year

Try with case when like below:

UPDATE Company_Coupon
SET Total_Coupons = @count
WHERE CompanyID = 1205
AND Month = (case when MONTH(GETDATE())-1=0 then 12 else MONTH(GETDATE())-1 end) AND Year = (case when MONTH(GETDATE())-1=0 then YEAR (GETDATE())-1 else YEAR (GETDATE()) end)

Get the records of last month in SQL server

SELECT * 
FROM Member
WHERE DATEPART(m, date_created) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(yyyy, date_created) = DATEPART(yyyy, DATEADD(m, -1, getdate()))

You need to check the month and year.

How to get previous month and year relative to today, using strtotime and date?

Have a look at the DateTime class. It should do the calculations correctly and the date formats are compatible with strttotime. Something like:

$datestring='2011-03-30 first day of last month';
$dt=date_create($datestring);
echo $dt->format('Y-m'); //2011-02

How to create a measure that show the count on previous month (PowerBI)

You can do it in Power Query and create a new table using this following code-

let your table name is your_old_table_name

let you have these 2 column there - date and value

Now create a new table **your_new_table_name" as below-

let
Source = your_old_table_name,
#"Grouped Rows" = Table.Group(Source, {"date"}, {{"_sum", each List.Sum([value]), type nullable number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each [Index] + 1),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Index"}, #"Added Custom", {"Custom"}, "Added Custom", JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"_sum"}, {"Added Custom._sum"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Added Custom",{"Index", "Custom"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Added Custom._sum", "previous_date_sum"}})
in
#"Renamed Columns"

You will have an output similar as below-

Sample Image



Related Topics



Leave a reply



Submit