Solved

Could you point how to format a date from YYYY/MM/DD HH:MM:SS to  DD/MM/YYYY HH:MM:SS  by using jQuery?

Posted on 2016-10-20
9
99 Views
Last Modified: 2016-10-26
Hi Experts

Could you point how to format a date from YYYY/MM/DD HH:MM:SS to  DD/MM/YYYY HH:MM:SS  by using jQuery?

Thanks in advance.
0
Comment
Question by:Eduardo Fuerte
  • 4
  • 3
  • 2
9 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41851983
You can use momentjs http://momentjs.com/
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.js"></script>
<script>
var x = '2016/10/20 12:12:26';
var y = moment(x).format(' DD/MM/YYYY HH:MM:SS');
console.log(y);
</script>

Open in new window

But if you do you will get this warning notice
Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:

Bottom line you need to look at why you need those formats and potentially change them.
0
 

Author Comment

by:Eduardo Fuerte
ID: 41852021
Really

The better strategy is to change it obtaining also formated from DB in the desired format.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41852025
Eduardo, please, please, please take a moment to learn the right way of handling date/time values.  This is not "new" or something that needs to be reinvented.  We have the ISO-8601 standard for date/time representations.  All internal computer programming date/time representations should be ISO-8601.  You only change the format for external display, full stop.  It's really, really, really worth doing it the right way because any other way leads to endless troubles, sadness, confusion, tears, and anguish.  Even little things, like the difference between slashes and dashes, can matter a lot, but there are safe, predictable ways to handle these values.  I promise that the "good ways" are your friend!

Here's the ten-year old procedural way:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

Here's the modern object-oriented way:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
2
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Eduardo Fuerte
ID: 41852511
@Ray

Thank you for your concerns.
I guess this case is a matter of jQuery date convertion. I don't see how to capture the textbox value by PHP code.
0
 

Accepted Solution

by:
Eduardo Fuerte earned 0 total points
ID: 41852695
In the meanwhile I found a javascript function for date format convertion:
<script type="text/javascript">
function formatDate(dateVal) {
    var newDate = new Date(dateVal);
    var sMonth = padValue(newDate.getMonth() + 1);
    var sDay = padValue(newDate.getDate());
    var sYear = newDate.getFullYear();
    var sHour = newDate.getHours();
    var sMinute = padValue(newDate.getMinutes());
    var sSeconds = newDate.getSeconds();
    
    if (sSeconds=="0") {
        sSeconds = "00";
    }
    var iHourCheck = parseInt(sHour);
    return sDay + "-" + sMonth + "-" + sYear + " " + sHour + ":" + sMinute  + ":" + sSeconds;
}

function padValue(value) {
    return (value < 10) ? "0" + value : value;
}
</script>

Open in new window


It runs OK.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41853082
Sorry, I misunderstood this to be a PHP question because it was in the PHP topic area.  The JS Date() method looks like the right solution.
0
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41853251
    return sDay + "-" + sMonth + "-" + sYear + " " + sHour + ":" + sMinute  + ":" + sSeconds;

Open in new window

= DD-MM-YY HH:MM:SS
NOT
DD/MM/YY HH:MM:SS

    return sDay + "/" + sMonth + "/" + sYear + " " + sHour + ":" + sMinute  + ":" + sSeconds;

Open in new window

0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41859980
Thank you for help!
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41860049
You are welcome.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question