2

Is there any way to get the number of milliseconds in a date field as either a String or Integer? So I pass 1000000000 into the date field, I dont want the date that represents, I want that number.

4
  • 1
    Ignoring the fact that a Date itself contains no meaningful time information...this sounds like an X-Y problem to me. Can you elaborate on why you think you need to convert a Date to a number of milliseconds? Also, milliseconds from what? the unix epoch?
    – Derek F
    Jan 17, 2017 at 15:52
  • Yeah the case is pretty weird. Basically I am making a template creation UI. So when you create a template and you have a date field I cant have the user put in an actual date. They need to put in the number of days in the future. So in this case say it is 60. But when the object is sent back to Apex it is a date field with 60 in it so it is just setting the field to Jan 1, 1970. I was hoping there was a way I can get the actual value, in milliseconds, returned to the field Jan 17, 2017 at 15:59
  • Yeah, you're definitely going about this the hard way. Do you have a controller or controller extension (in apex) that you're using to back up your UI? If so, can you edit your question to include that code?
    – Derek F
    Jan 17, 2017 at 16:08
  • Well its the controller Im working on. Right now im trying to figure it how (or if) I can get the value I send back in that date field. If not Im gonna have to go back to the drawing board. I am looking at alternative ways to send across the object data Jan 17, 2017 at 16:34

3 Answers 3

2

I assume you are talking about a Datetime field, as the timestamp representation of a date isn't very meaningful. If you want to get the timestamp representation and the data type Long is also suitable for you, you can use the getTime-method of the Datetime-class, which is described here: Datetime getTime

getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

Signature

public Long getTime()

Return Value

Type: Long

DateTime dt = DateTime.newInstance(2007, 6, 23, 3, 3, 3);
Long gettime = dt.getTime();
Long expected = 1182592983000L;
System.assertEquals(expected, gettime);

Edit: If it is a Date-field, you could also convert it an use a fixed time of the day for this conversion. This can be achieved using the newInstance(date, time)-method of the Datetime-class.

2

If you want to get the date a certain number of days in the future, you need two Date methods: today and addDays.

Date target = Date.today().addDays(60);
1

If you truly can't allow your users to input a future date using the standard date picker that you'd get when using a Date field in an <apex:inputField>, then Adrian's answer explaining how to use Date.addDays() is the right way to go about this. The rest is simply a shift in your UI perspective.

If you're asking your users 'how many days in the future do you want this thing to be?', then your UI shouldn't be asking for a date, it should be asking for an integer.

If the sObject that you're working with has a Number field that you can use, then most of the work is simply updating your visualforce page to take the input, and store it in your chosen field.

If you don't have such a field available, or aren't working with an sObject, then you'll need to add an attribute/property to your controller like so

public class MyController{
    // We need to specify both a getter and a setter for this to work.
    // This is the simplest way to define a getter & setter.
    public Integer numDays {get; set;}

    // other controller stuff, like a constructor
}

The unfortunate bit about using something other than an sObject field is that you won't get an automatic label for the input field in your visualforce page. You'll have to make the field label yourself. Otherwise, using a controller property in <apex:input> is very similar to referencing a field in an <apex:inputField>

<apex:form>
    <apex:input value="{!numDays}" type="Number" label="Delay x Days" />
</apex:form>

The final bit (in both cases) is using this data to calculate your target date. To do this, you'll most likely want to override the save() method from the standard controller in your extension.

public class MyController{
    // We need to specify both a getter and a setter for this to work.
    // This is the simplest way to define a getter & setter.
    public Integer numDays {get; set;}

    // other controller stuff, like a constructor

    // This is how you override the save method.
    // When you click the 'save' button, this method will be called instead
    //   of the standard controller's save method
    public pageReference save(){
        // Salesforce hides the details, but inside this method, you'll
        //   have access to the number that your user put into the page via
        //   'numDays'

        // And here we see the usage of addDays that Adrian suggests (with
        //   one small change)
        // I assume that your controller extension has a `myObject` attribute,
        //   that it is a reference to an sObject, and that it has a 'target_Date__c'
        //   field
        myObject.target_Date__c = Date.Today().addDays(this.numDays);

       // Assuming your controller extension is storing the standard controller 
       //   instance, you can let the standard controller's save() method take care of the rest
       return this.standardController.save();
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .