Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to remove time portion of date using javascript?

    i have a javascript variable which holds date time value... i need only date from it...

    Code:
    <script type="text/javascript">
        function update(){
        var datefield = document.getElementById("myTextBox_CWPWORKLIST.DUE_DATE").value;
        alert(datefield);
        }
        </script>

    here the datefield = 2013-02-21 00:00:00.0... i need datefield = 2013-02-21.. how to get this..

  • #2
    New to the CF scene
    Join Date
    Jan 2013
    Location
    United States
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    var datefield = document.getElementById("myTextBox_CWPWORKLIST.DUE_DATE").value;
    var s = datefield;
    var n = s.slice(0, -10);
    alert(n);


    This code definitely meets your requirements i think.

    Thank you.

  • Users who have thanked xhtmlchamps1 for this post:

    BobbyRachel (01-24-2013)

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    here is my answer:

    Code:
    <script type="text/javascript">
        function update(){
        var datefield = document.getElementById("myTextBox_CWPWORKLIST.DUE_DATE").value;
        alert(datefield);
    var columnname = dateFormat(datefield, "dd-mm-yy");
    alert(datefield);
        }
        </script>

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by BobbyRachel View Post
    here is my answer:

    Code:
    <script type="text/javascript">
        function update(){
        var datefield = document.getElementById("myTextBox_CWPWORKLIST.DUE_DATE").value;
        alert(datefield);
    var columnname = dateFormat(datefield, "dd-mm-yy");
    alert(datefield);
        }
        </script>
    Your answer is incomplete - you haven't included the code for the dateFormat function you created and the part of the code that actually does what the OP asked is presumably somewhere in that function.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Ummm...Felgall: The "OP" here is the same person who wrote the non-answer that you commented on.

    I think we have to assume he stole...err...copied...a dateFormat() function from someplace.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    I think we have to assume he stole...err...copied...a dateFormat() function from someplace.
    Which will not help the next person coming here searching for an answer to the same question as there still isn't an actual complete solution to the question published.

    Since none of the code here actually validates what the field contains at all the first step would be to validate that the field actually contains a date/time and to load that into an appropriate JavaScript variable using new Date(). Then you can easily extract just the date portion using the appropriate methods.

    Neither version of the code the OP posted would work if the person entered "noon yesterday" as their date and time even though the code they have would allow that input.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    LOL! Well, crud. And here I wanted to be able to enter "my next birthday" and have JavaScript figure it out! What a truly worthless language. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    LOL! Well, crud. And here I wanted to be able to enter "my next birthday" and have JavaScript figure it out!
    There's nothing in the OP's code to prevent you entering that and they claimed that everything now works so presumably the dateFormat function knows when your next birthday is.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    LMAO! Touchė. I really and truly laughed out loud, not just grinned, when I read that!

    Best laugh I've had all week except when my 2.5 year old granddaughter said "My cup disappeared." (You had to be there. It was mainly the shock of the big word, but the cup was right behind her.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •