DateTimes年份的差异

时间:2014-09-02 06:21:44

标签: c# android xamarin

我在Xamarin中创建了一个DatePicker,我遇到了问题。我试图通过添加出生的年,月和日来制作计算人年龄的代码。但是,调试后的代码在我将年数转换为字符串时停止,给出了错误:"抛出了Android.Content.Res.Resources + NotFoundException。字符串资源ID#0x2"

Xamarin代码:

        base.OnCreate (bundle);

        SetContentView (Resource.Layout.AddDataLayout);

        Button regbutton = FindViewById<Button> (Resource.Id.regButton);
        DatePicker date = FindViewById<DatePicker> (Resource.Id.datePicker);

        //regbutton.Enabled = false;



        regbutton.Click += delegate 
        {
            DateTime zeroTime = new DateTime(1, 1, 1);
            DateTime BornDate = date.DateTime;
            DateTime sToday = DateTime.Today;
            TimeSpan span = sToday - BornDate;
            int years = (zeroTime + span).Year - 1;
            string a = Convert.ToString(years);
            Toast.MakeText(this,a , ToastLength.Long).Show();
            this.Finish();
        };

1 个答案:

答案 0 :(得分:-2)

Calendar firstCalendar = Calendar.getInstance();        

Calendar secondCalendar = Calendar.getInstance();

long diff = currentCalendar.getTimeInMillis() - calendar.getTimeInMillis();
int sec = (int) (diff/1000);
int min = sec/60;
int hr = (int) (min/60);
int days = hr/24;
int month = days/30;
int years = month/12



if(sec>60){
        if(min>60){
            if(hr>24){
                if(days>30){
                    if(month>12){
                        submitDiffrence = Integer.toString(month)+" Years ago";
                    }else{
                        submitDiffrence = Integer.toString(month)+" month ago"; 
                    }
                }else{
                    submitDiffrence = Integer.toString(days)+" days ago";
                }
            }else{
                submitDiffrence = Integer.toString(hr)+" hour ago";
            }
        }else{
            submitDiffrence = Long.toString(min)+" min ago";
        }
    }else{
        submitDiffrence = Long.toString(sec)+" sec ago";
    }