如何将当前系统的年份存储或存储到变量中?

时间:2015-10-03 11:54:12

标签: vb.net combobox

这是我的计划的概念。 有一个组合框,有一年(从1915年至今)和textBox,将显示年龄。

这是我的想法

Dim today as integer'this is where i want to store the current year    
Dim year as integer'stores selected items from combobox
Dim age as Integer      
year = combobox.selectedItem
age = year - today 
txtbox.text = age 'displays age on textbox

2 个答案:

答案 0 :(得分:1)

获得实际年份:Today.year。更多:您需要将字符串传递给TextBox。年龄是整数,因此您需要转换为字符串。我试过这个,它似乎正常工作:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // Creating a new RelativeLayout
        RelativeLayout ll = new RelativeLayout(this);
        ll.setBackgroundColor(getResources().getColor(R.color.red));

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        layoutParams.setMargins(10, 10, 10, 10);


        setContentView(ll);
}

答案 1 :(得分:0)

如果您需要显示年龄,了解出生月份很有意思,因为根据当月和当月,您将有1年的差异(更多或更少)。

尝试这样的事情:

   Dim MyBirth As String = Inputbox("When had you borned (dd/mm/yyyy)?")
   Dim Age As String = DateDiff(DateInterval.Year, CDate(MyBirth), Today).ToString

   msgbox( "You are " & Age & " years old")