?
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
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 58

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 111

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 111

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 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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 58

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

771 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