Skip to content
Advertisement

How to generate this format number YYYYMM0001?

I need to generate a set of number consist of Current Year, Current Month, and Incremental 4 digit with zero leading. with conditions:

  1. The last 4 digit number is increment from the last record on database.

example:

JavaScript
  1. on beginning of month the last 4 digit reset to 0001.

example:

JavaScript

this is what i have so far:

JavaScript

Question how to increment the last 4 digit from last record and how to reset to 0001 on beginning of month?

Advertisement

Answer

The solution requires the last formatted ID and a little logic. If the last ID is in the current month, it only needs to be increased by 1. If this is not the case, the new month starts at 1.

JavaScript

examples:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement