Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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?

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
Eduardo Fuerte
Asked:
Eduardo Fuerte
  • 4
  • 3
  • 2
2 Solutions
 
Julian HansenCommented:
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
 
Eduardo FuerteAuthor Commented:
Really

The better strategy is to change it obtaining also formated from DB in the desired format.
0
 
Ray PaseurCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Eduardo FuerteAuthor Commented:
@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
 
Eduardo FuerteAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
    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
 
Eduardo FuerteAuthor Commented:
Thank you for help!
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now