Date Tool

Graham Lewis's Avatar

Graham Lewis

28 Apr, 2014 04:01 PM

I am trying to format a date generated via a DataDefinition - it is writing it as 06-01-2014, I tried..

#set ($pubDate = $_DateTool.getDate($item.getChild('last-published-on').value))

But it gave me an error saying it couldn't parse it. I want to change to June 1, 2014 or something like that.

  1. 1 Posted by Eric L. Epps on 28 Apr, 2014 04:39 PM

    Eric L. Epps's Avatar

    More of a code sample would be helpful. What is the content of the $item variable? I assume you're using the sample code from the KB, but that's not specific to Data Definitions.

  2. 2 Posted by Ryan Griffith on 28 Apr, 2014 06:05 PM

    Ryan Griffith's Avatar

    Hi Graham,

    The DateTool.getDate method is expecting an integer and not a String. In your case you will need to use the DateTool.toDate method to generate the Java Date object, which will allow you to specify the format of the string you are passing in.

    When you have a moment, give the following a try and let me know how it works out:

    #set ($date = $_DateTool.toDate("MM-dd-yyyy", $item.getChild('last-published-on').value))
    $_DateTool.format("MMMM d, yyyy", $date)
    

    Note: I am assuming the value of the field is MM-dd-yyyy. If not, feel free to modify the pattern accordingly. More information about patterns accepted by the Date Tool can be found here).

    Please let me know if you have any questions.

    Thanks!

  3. 3 Posted by Graham Lewis on 28 Apr, 2014 06:30 PM

    Graham Lewis's Avatar

    Once again, you got it. Thanks!

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  4. Tim closed this discussion on 29 Apr, 2014 02:43 PM.

  5. Graham Lewis re-opened this discussion on 28 Jul, 2014 07:51 PM

  6. 4 Posted by Graham Lewis on 28 Jul, 2014 07:51 PM

    Graham Lewis's Avatar

    Sorry to reopen this, how do you have the date picker in the DD display on the page as July 28, 2014 instead of the default 7/28/14?

  7. 5 Posted by Ryan Griffith on 29 Jul, 2014 04:11 PM

    Ryan Griffith's Avatar

    Hi Graham,

    To clarify, are you referring to the formate of the text field itself when selecting a date in the calendar widget? If so, this is not currently possible. That being said, please feel free to post a suggestion on our Idea Exchange for others to vote up and/or comment on.

    Please let me know if you have any questions.

    Thanks!

  8. 6 Posted by Graham Lewis on 29 Jul, 2014 04:39 PM

    Graham Lewis's Avatar

    I'm talking about in the resulting data after it is selected. I want it to show the actual month instead of the numbers.

    I will for sure go post something about selecting the format of the calendar widget, that would be awesome.

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  9. 7 Posted by Ryan Griffith on 29 Jul, 2014 04:41 PM

    Ryan Griffith's Avatar

    Ah, thank you for clarifying. In terms of the data being generated within an Index Block, it is not possible. If you are talking about formatting the date within say a Velocity Format, you can use the same method I outlined previously, but adjust the format that is provided to the toDate() method.

    Please let me know if you have any questions.

    Thanks!

  10. 8 Posted by Graham Lewis on 29 Jul, 2014 05:40 PM

    Graham Lewis's Avatar

    I thought I did that, here is my format.

    @@@

    #foreach ( $event in $events )
    #set ($date = $_DateTool.toDate($event.getChild('date').value))
    #set ( $title = $event.getChild("event") )
    <div>
    <h2>$_DateTool.format('MM/dd/yy', $date)</h2>
    <p style="font-size:24px">${_EscapeTool.xml($title.value)}</p>
    </div>
    #end
    @@@

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  11. 9 Posted by Ryan Griffith on 29 Jul, 2014 05:45 PM

    Ryan Griffith's Avatar

    Hm, it looks like you're not using the correct formatting options when displaying the date. When you have a moment, try the following:

    $_DateTool.format("MMMM d, yyyy", $date)
    

    Please let me know if you have any questions.

    Thanks!

  12. 10 Posted by Graham Lewis on 29 Jul, 2014 05:47 PM

    Graham Lewis's Avatar

    That just wrote that out. I bet I'm missing something else in my format.

    here is the whole thing

    @@@

    #set ( $events = $_XPathTool.selectNodes($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/events") )
    #set ( $date = $text.getChild("heading") )

    <!-- You may want to "minify" these after making edits http://www.refresh-sf.com/yui/ -->
    <link rel="stylesheet" href="http://www.jsu.edu/_common/_files/signage/content-slider/animate.css" /> <!-- Optional -->
    <link rel="stylesheet" href="http://www.jsu.edu/_common/_files/signage/content-slider/liquid-slider.css" />

    <!-- Liquid Slider relies on jQuery -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <!-- More scripts below! -->

    [system-view:external]

    <div class="liquid-slider" id="main-slider" >
    #foreach ( $event in $events )
    #set ($date = $_DateTool.toDate($event.getChild('date').value))
    #set ( $title = $event.getChild("event") )
    <div>
    <h2>$_DateTool.format("MMMM d, yyyy", $date)</h2>
    <p style="font-size:24px">${_EscapeTool.xml($title.value)}</p>
    </div>
    #end
    </div>

    [/system-view:external]

    [system-view:internal]

    <div class="liquid-slider" id="main-slider" >
    #foreach ( $event in $events )
    #set ($date = $_DateTool.toDate("MM-dd-yyyy", $event.getChild('date').value))
    #set ( $title = $event.getChild("event") )
    <div>
    <h2>$_DateTool.format("MMMM d, yyyy", $date)</h2>
    <p style="font-size:16px">${_EscapeTool.xml($title.value)}</p>
    </div>
    #end
    </div>

    [/system-view:internal]

    <!-- Feel free to load scripts in the footer -->
    <script src="http://www.jsu.edu/_common/_files/signage/content-slider/jquery.easing.1.3.js"></script>
    <script src="http://www.jsu.edu/_common/_files/signage/content-slider/jquery.touchSwipe.min.js"></script>
    <script src="http://www.jsu.edu/_common/_files/signage/content-slider/jquery.liquid-slider-custom.min.js"></script>
    <script>
    $('#main-slider').liquidSlider();

    /* If you need to access the internal property or methods, use this:
    var api = $.data( $('#slider-id')[0], 'liquidSlider');
    */
    </script>
    @@@

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  13. 11 Posted by Ryan Griffith on 29 Jul, 2014 05:51 PM

    Ryan Griffith's Avatar

    You may need to change the toDate() call and provide it with a format for the date string you are passing into it. Try changing:

    #set ($date = $_DateTool.toDate($event.getChild('date').value))
    

    To:

    #set ($date = $_DateTool.toDate("MM-dd-yyyy", $event.getChild('date').value))
    

    Please let me know if you have any questions.

    Thanks!

  14. 12 Posted by Graham Lewis on 29 Jul, 2014 06:10 PM

    Graham Lewis's Avatar

    Once again...3 points to you. I'll get this one day...I promise :)

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  15. 13 Posted by Ryan Griffith on 29 Jul, 2014 07:07 PM

    Ryan Griffith's Avatar

    All good, Graham, I'll give you an A for effort either way.

    Have a great day!

  16. Ryan Griffith closed this discussion on 29 Jul, 2014 07:07 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac