如何根据年份和月份增加数字

时间:2016-06-16 18:26:21

标签: c#

我以yyMM001格式在数据库中存储一个int,其中" yy"是当年的最后两位数," MM"当前月份是两位数,最后三位数应该从001增加到月末。并且下个月的MM值应该更改为当前的月份数字,最后三位数字应该再次从001重新开始。 以下是代码:

        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;
        }

我应该在if条件下使用哪种方式检查当前月份并更改月份数​​字,最后三位数字应该从001重启? 如果有人知道,请帮助我。感谢

4 个答案:

答案 0 :(得分:1)

如果我理解这个问题,这段代码应该对你有用。如果月份和年份匹配,则取值并将其递增1.如果不匹配,则再次从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;

或者,使用数字而不是字符串

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;

两者之间的性能差异可以忽略不计,但数字版本稍快一些。

答案 1 :(得分:1)

此方法一旦给出当前Forn_No将生成下一个

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;
}

并像这样使用

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;
}

答案 2 :(得分:0)

试试这个:

perl_set $test_rdns "sub { use Socket; return scalar \
                gethostbyaddr(inet_aton($remote_addr), AF_INET); }";

你只需要 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); } ;

答案 3 :(得分:0)

您最好将自定义号码转换为DateTime,添加一天,然后转换回自定义格式。

从自定义格式转换为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;