Powershell - 让人变老

时间:2013-02-19 14:03:32

标签: powershell

我试图让一个人年龄,月,日,小时,分钟和秒。

这就是我到目前为止所得到的(你可以告诉我,我是一个初学者):

$now = [datetime]::now
[datetime]$birthday = "12/22/2012 03:22:00"
$age = [datetime]$now - $birthday

Write-Host "My daughter's age is:" $age.Days "days" ($age.Hours) "hours" ($age.Minutes) "minutes" ($age.Milliseconds) "seconds"

输出结果为:

My daughter's age is: 59 days 10 hours 27 minutes 76 seconds

没关系,但如果输出更“人性化”,那就太棒了:

1 Month 19 Days 10 hours 28 minutes 16 seconds

这实际上比我最初预期的要困难得多。代码必须知道每个月有多少天才能将天数分成几个月和几天。并且还要考虑今年是闰年!

也许PowerShell可以为它提供一些神奇的帮助!

谢谢!

2 个答案:

答案 0 :(得分:0)

我相信这就是你所追求的目标。

[datetime]$birthday = "12/22/2012 03:22:00"
$span = [datetime]::Now - $birthday
$age = New-Object DateTime -ArgumentList $Span.Ticks

Write-Host "My daughter's age is:" $($age.Year -1) Years $($age.Month -1) Months $age.Day "days" ($age.Hour) "hours" ($age.Minute) "minutes" ($age.second) "seconds"

答案 1 :(得分:0)

在生日后的第二天检查。有一天多了!

正确的是:

public class LoginSoapTask extends AsyncTask<Void, Void, Void> {
    /*Web Service Details */

    private String xmlProlog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    public String username, password;

    Context context;
    public LoginSoapTask(Context c) {
        context = c;
    }
    private final ProgressDialog dialog = new ProgressDialog(context); //THIS IS WHERE I AM HAVING THE PROBLEM!!!!!!!!! (Line 30)
    protected void onPreExecute() {

        this.dialog.setMessage("Verifying...");
        dialog.show();
    }


    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    protected void onPostExecute(Void result) {
        // TextView tv = (TextView) findViewById(R.id.Result);
        //  tv.setText(test_string);
        System.out.println(test_string);
        if (this.dialog.isShowing()){
            this.dialog.dismiss();
         }
    }

    private String test_string;

    @SuppressWarnings("deprecation")
    public void WebServiceCallExample() {
        String finalInput = "1:" + username.length() + ":" + username + ":2:" + password.length() + ":" + password + ":";
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("credentials", finalInput);

        /* Set the web service envelope */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        System.out.println("Request::" + Request.toString());

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        try{
            System.out.println("Lghung:" + SOAP_ACTION);
            httpTransport.debug=true;

            httpTransport.call(SOAP_ACTION, envelope);
            String ss=httpTransport.responseDump;
            //SoapObject sp = (SoapObject) envelope.getResponse();
             SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
            System.out.println("Response::" + resultsRequestSOAP.toString());
            System.out.println(Request.getName());
             test_string = xmlProlog + response.toString();
             System.out.println(test_string);
        }catch (Exception e){
            System.out.println("Testing");
            e.printStackTrace();
        }

    }

    public void checkDetails(String givenUsername, String givenPassword) {
        username = givenUsername;
        password = givenPassword;
        execute();
    }
}