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
23 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 51

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 108

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 108

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 51

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 51

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now