Prashant Arora
Prashant Arora

Reputation: 86

How to increment a number according to the year and month

I am storing an int in the database in the format yyMM001 where "yy" is the last two digits of the current year, "MM" is the current month in two digits and last three digits should increase from 001 till the month end. and the next month MM value should be change to the current months digits and last three digits should restarts from 001 again. Below is the code :

        string year = System.DateTime.Now.ToString("yy");
        string month = System.DateTime.Now.ToString("MM");
        int no = 0;
        string frmno;
        IUD.Query = "Select MAX(Form_No) AS Form_No From tbl_Students";
        DataTable dtfm = IUD.FetchToDataBase();
        if (dtfm.Rows.Count > 0 && dtfm.Rows[0]["Form_No"].ToString() != string.Empty)
        {
            no = int.Parse(dtfm.Rows[0]["Form_No"].ToString()) + 1;
            return no;
        }
        else
        {
            frmno = year + month + "001";
            no = int.Parse(frmno);
            return no;
        }

What way should I use in the if condition to check the current month and change the months digits and last three digits should be restart from 001? Please help me if anyone knows. Thanks

Upvotes: 1

Views: 2286

Answers (4)

Nkosi
Nkosi

Reputation: 247413

This method once given the current Forn_No will generate the next one

public static string getNext(string Form_No) {
    //Template: {YY}{MM}001 to {YY}{MM}999
    var next = "";
    var prefix = Form_No.Substring(0, 4);
    var nextNumber = Int32.Parse(Form_No.Substring(4)) + 1;
    var aux = nextNumber.ToString("D3");
    next = string.Format("{0}{1}", prefix, aux);
    return next;
}

And used like this

var date = System.DateTime.Now;
string prefix = date.ToString("yyMM");
string frmno;
IUD.Query = "Select MAX(Form_No) AS Form_No From tbl_Students WHERE Form_No LIKE " + prefix + "%";
DataTable dtfm = IUD.FetchToDataBase();
if (dtfm.Rows.Count > 0 && dtfm.Rows[0]["Form_No"].ToString() != string.Empty)
{
    var max = dtfm.Rows[0]["Form_No"].ToString();
    frmno = getNext(max);
    return frmno;
}
else
{
    frmno = prefix + "001";
    return frmno;
}

Upvotes: 1

Spivonious
Spivonious

Reputation: 718

If I understand the question, this code should work for you. If the month and year match, it takes the value and increments it by 1. If not, it starts again at 001.

var yearMon = dtfm.Rows[0].ToString().Substring(0,4);
if (yearMon == DateTime.Now.ToString("yyMM")) {
    no = dtfm.Rows[0] + 1; //assuming the database column is an int and not a varchar
} else {
    no = Int32.Parse(DateTime.Now.ToString("yyMM001"));
}

return no;

Or, using numbers instead of strings

var now = DateTime.Now;
var decade = now.Year % 100;
var month = now.Month;
var decadeMonth = decade * 100 + month;

no = dtfm.Rows[0] / 1000 == decadeMonth ? dtfm.Rows[0] + 1 : decadeMonth * 1000 + 1;

The performance difference is negligible between the two, but the numbers version is slightly faster.

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 134045

You're probably better off converting your custom number to a DateTime, adding a day, and then converting back to your custom format.

Converting from your custom format to a DateTime

int myCustomDate = 1606016;  // June 16, 16

int day = myCustomDate % 1000;
int month = (myCustomDate / 1000) % 100;
// Somehow you have to give it a century. Since you're using 2-digit years,
// I'll assume current century.
int year = 2000 + (myCustomDate / 100000);
DateTime dt = new DateTime(year, month, day);


dt.AddDays(1);

// then convert back to your number
int newCustomDate = (100000 * dt.Year) + (1000 * dt.Month) + dt.Day;

Upvotes: 0

Lucian Bumb
Lucian Bumb

Reputation: 2881

try this:

 for (var i = 0; i < 999 + 1; i++)
            {
                var yy =DateTime.Now.ToString("yy");
                var mm = DateTime.Now.ToString("MM");

                var num = $"{yy}{mm}{i.ToString("D3"),3}";
                Console.WriteLine(num);
            }

you just need to make $"{yy}{MM}{yourNumber}";

Upvotes: 0

Related Questions