문제

1-1970 년 이후 서버로부터 밀리 초의 날짜를 받고 있습니다. 그런 다음 DateFormatter를 사용하여 날짜를 화면에 인쇄합니다. 그러나 Flex는 시간의 시간을 추가하므로 서버에서 얻은 것과 다른 시간을 표시합니다. 인쇄하기 전에 날짜를 변경 하여이 문제를 해결했습니다. 그러나 날짜 개체가 올바른 날짜를 보유하지 않기 때문에 그것이 나쁜 해결책이라고 생각합니다.

시간대를 무시하고 날짜를 인쇄하여 날짜를 인쇄하는 방법을 아는 사람이 있습니까?

이것이 내가 한 방법입니다.

function getDateString(value:Date):String
{
    var millisecondsPerMinute:int = 1000*60;
    var newDate:Date = new Date(value.time - (millisecondsPerMinute*value.timezoneOffset));

    var dateFormatter:DateFormatter = new DateFormatter();
    dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";

    return dateFormatter.format(newDate);
}
도움이 되었습니까?

해결책

어쩌면 내가 놓친 것이 있을지 모르지만 이것은 나에게 효과가있는 것 같습니다.

<?xml version="1.0"?>
<!-- formatters\FormatterDateField.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<!-- Declare a DateFormatter and define formatting parameters.-->
<mx:DateFormatter id="dateFormatter" 
    formatString="EEEE DD-MM-YYYY LL:NN:SS AA"/>

<mx:Label text="Millis (1220836618601 == Monday 08-09-2008 01:16:58 AM):"/>
<mx:TextInput id="dob" text="1220836618601"/>

<mx:Label text="Formatted date UTC: "/>
<mx:TextInput id="formattedDate" 
    text="" 
    editable="false"/>
<mx:Label text="Formatted date local: "/>
<mx:TextInput id="formattedDateLoc" 
    text="" 
    editable="false"/>

<!-- Format and update the date.-->
<mx:Button label="Format Input" 
    click="
        var d :Date = new Date(parseInt(dob.text));
        formattedDate.text=dateFormatter.format(d.toUTCString());
        formattedDateLoc.text=dateFormatter.format(d);
    "/>
</mx:Application>

날짜 객체 (시간대 종속)를 dateformatter에 전달하는 대신 날짜 개체의 UTC 문자열을 대신 전달하도록 제안하십시오. DateFormatter가 시간대에 어떤 일이 발생한다는 것을 제안하는 것은 없었으므로, 특히 날짜 개체가 이미 UTC를 얻는 방법을 제공 할 때 시간대를 보상하려고 할 필요가 없습니다.

function getDateString(value:Date):String
{
    var dateFormatter:DateFormatter = new DateFormatter();
    dateFormatter.formatString = "EEEE DD-MM-YYYY LL:MM AA";

    return dateFormatter.format(value.toUTCString());
}

다른 팁

Flex Hero 4.5에서는 새로운 것을 사용할 수 있습니다 Spark DateTimeFormatter:

<s:DateTimeFormatter dateTimePattern="HH':'mm':'ss" id="dateFormatterUTC" useUTC="true" />
<s:Label text="{dateFormatterUTC.format(new Date())}" />

가장 간단한 수정 사항은 가능한 많은 객체 (및 객체의 속성)가 문자열이되는 것입니다. TimeZoneOffset 솔루션은 잘 작동하지만 많은 미국 도시의 TimeZoneOffset은 연중 두 번 변경됩니다. 가장 좋은 규칙 - 모든 것이 문자열입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top