2014-07-23 85 views
0

我必須將標準時間轉換爲動作中的傳統時間。這是我到目前爲止。我將衷心感謝您的幫助。如何在ActionScript 3.0中將標準時間HH:MM:SS轉換爲傳統時間

// This line makes the button, btnConvert wait for a mouse click 

// When the button is clicked, the convertTime function is called 

btnConvert.addEventListener(MouseEvent.CLICK, convertTime); 

// This line makes the textinput wait for a mouse click 

// When this component is clicked, the clearLabels function is called 

txtinStandard.addEventListener(MouseEvent.CLICK, clearLabels); 

// Declare Global Variables 

var traditionalTime:String; // traditional time 

// This is the convertTime function 

// e:MouseEvent is the click event experienced by the button 

// void indicates that the function does not return a value 

function convertTime(e:MouseEvent):void 
{ 

// declare the variables 
    var standardTime:String; // standard time entered by user 

    standardTime = txtinStandard.text;  // get standard time from user 
    convertToTraditional(standardTime); // call the convertToTraditional function 

    // output an appropriate message in the label 
    lblOutput.text = standardTime + " is equivalent to " + traditionalTime; 
} 

// This is function convertToTraditional 

// s – is the standard time 

// It determines the traditional time based on the value of s 

function convertToTraditional(s:String){ 

} 

// This is the clearLabels function 

// e:MouseEvent is the click event experienced by the textInput 

// void indicates that the function does not return a value 

function clearLabels(e:MouseEvent):void 

{ 

    lblOutput.text = ""; 

    txtinStandard.text = ""; 
} 
+1

你能澄清你的標準時間與傳統的時間是什麼意思? – Marcela

回答

1

假設你的意思是標準時間是24小時制和傳統的時間是AM/PM時鐘:

function convertTime(e:MouseEvent):void 
{ 

    // declare the variables 
    var standardTime:String; // standard time entered by user 

    standardTime = txtinStandard.text;  // get standard time from user 
    traditionalTime = convertToTraditional(standardTime); // call the convertToTraditional function 

    // output an appropriate message in the label 
    lblOutput.text = standardTime + " is equivalent to " + traditionalTime; 
} 

function convertToTraditional(s:String):String{ 
    // separate the standard time into it parts (hour, minutes, seconds) 
    var parts:Array = s.split(":"); 

    if(parts.length != 3) 
    { 
     return "invalid time. require HH:MM:SS"; 
    } 

    var hour:int = int(parts[0]); 
    var min:int = int(parts[1]); 
    var sec:int = int(parts[2]); 

    // check for errors in each of the time parts 
    if(hour > 23 || min > 59 || sec > 59) 
    { 
     return "invalid time"; 
    } 

    // determine if the hour is PM or AM 
    var am_pm:String = hour >= 12 ? "PM" : "AM"; 

    // modding the hour by 12 will give you the correct traditional hour 
    hour %= 12; 
    if(hour == 0) hour = 12; // 0 is a special case 

    // create the padding for each time part (prepend with a 0 if it's less than 10) 
    var hourPad:String = hour < 10 ? "0" : ""; 
    var minPad:String = min < 10 ? "0" : ""; 
    var secPad:String = sec < 10 ? "0" : ""; 

    // string it all together 
    return hourPad + hour.toString() + ":" + minPad + min.toString() + ":" + secPad + sec.toString() + " " + am_pm; 
} 
+1

由於要求的人是初學者,因此您的答案很有用,因爲它顯示了一些字符串解析和條件(+1)。對於更高級的用戶,「DateTimeFormatter」類更有意義 –

3

你可以試試內置AS3工具這一點。

private function convertToTraditional(s:String):String { 
     const date:String = "01/01/1970 "; 
     var curTime:Date = new Date(Date.parse(date + s)); 
     var df:DateTimeFormatter = new DateTimeFormatter("en_US"); 
     df.setDateTimePattern("h:mm:ss a"); 
     return df.format(curTime); 
    } 

Date.parse()

dateTimeFormatter.setDateTimePattern()

相關問題