2

在我的 Salesforce 应用程序中,我希望用户输入日期/时间值,但时间部分是可选的。有什么办法可以做到这一点。谢谢。

4

3 回答 3

2

As a solution, rather than using custom object filed type DateTime value, I have keep separate Date value and Time value in VF controller level and with the compose those values make a DateTime.

Apex code is as bellow,

 // Compos DateTime with Date & Time values 
    this.jobOccurrence.Date_Time_Sampled__c = DateTime.parse(this.dateSampled.trim() + ' ' + this.timeSampled.trim());

VisualForce code is as bellow,

<apex:page>

   <!-- Style sheet for timepicker -->
   <link rel="stylesheet" media="all" type="text/css" href="{!URLFOR($Resource.jquery_ui_custom)}" />

   <style>
       #ui-datepicker-div, .ui-datepicker{ font-size: 70%; }     
      .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
      .ui-timepicker-div dl { text-align: left; }
      .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
      .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
      .ui-timepicker-div td { font-size: 90%; }
      .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; 

   </style>


   <!-- javascript for timepicker -->
   <script type="text/javascript" src="{!URLFOR($Resource.jquery)}"></script>
   <script type="text/javascript" src="{!URLFOR($Resource.jquery_ui_custom_min)}"></script>
   <script type="text/javascript" src="{!URLFOR($Resource.jquery_ui_timepicker_addon)}"></script>

   <script type="text/javascript">  

     /* Set Timepicker to component */    
     function setTimepicker(componentObj) {      
      $(componentObj).timepicker({ show24Hours: false, timeFormat: 'hh:mm TT', ampm: true});          
     }
   </script> 

 <apex:form id="frmMain">

   <!-- Date -->                       
   <apex:inputText value="{!dateSampled}" onfocus="DatePicker.pickDate(false, this.id, false);"/>
   <!-- Time --> 
   <apex:inputText value="{!timeSampled}" onfocus="setTimepicker(this);"/> 

 </apex:form>

</apex:page>
于 2012-10-17T04:41:49.147 回答
2

您能否不使用两个字段(一个日期和另一个时间文本字段)。然后将这些字段连接成一个日期时间公式字段。这不会有帮助吗?

于 2012-10-06T19:28:22.907 回答
2

如果时间是可选的,只需使用 00 :

Datetime myDate = datetime.newInstance(2012, 10, 5, 00, 00, 0);
于 2012-10-05T06:24:01.270 回答