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
296 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 57

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 110

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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 

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 110

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 57

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 57

Expert Comment

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

717 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