Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

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.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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.
Avatar of Eduardo Fuerte

ASKER

Really

The better strategy is to change it obtaining also formated from DB in the desired format.
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
@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.
ASKER CERTIFIED SOLUTION
Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for help!
You are welcome.