Solved

jQUERY DATE FORMAT

Posted on 2013-01-19
6
1,941 Views
Last Modified: 2013-03-09
Hi,
I am new to jquery and need to use a date formatted in the  UK style I am using  var date1 = new Date(); to obtain the date and have attempted to convert the date returned 'Sat Jan 19 2013 16:52:39 GMT+0100 (W. Europe Standard Time)'  to the format 'dd/mm/yy hh:mm' how to I achive this?
0
Comment
Question by:JoeBo747
6 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 250 total points
Comment Utility
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 250 total points
Comment Utility
You can just ust the toString() function I think and it will go to the local time zone
<script>
var date1 = new Date(); 
var date2=date1.toString();
$('div.time').text(date2);
</script>
 <div class="time"></div>

Open in new window

and for reference some help below
http://docs.jquery.com/UI/Datepicker/formatDate
http://www.w3schools.com/jsref/jsref_obj_date.asp
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Here is the top link in google for this FAQ
http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript

it mentions moment.js which is the library to use if you don't already use jQuery UI
http://momentjs.com/
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 25

Expert Comment

by:Kyle Hamilton
Comment Utility
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
    </head>
    <body>
        <div id="date"></div>
        <script>
            
           ( function writeDate(){
            var date    = new Date(),
                month   = date.getMonth() + 1, // 0-11,
                day     = date.getDate(), 
                year    = date.getFullYear(),
                hour    = date.getHours(),
                minute  = date.getMinutes();
            
                year = year.toString().slice(2,4);
            
            
                var dateContainer = document.getElementById("date");
                dateContainer.innerHTML = day+"/"+month+"/"+year+" "+hour+":"+minute;
            })();
            </script>
    </body>
</html>

Open in new window

Do you want to include leading 0's?

In this example the script is at the bottom of the page so you don't need an onload event. If you want to put this script at the top of your page, you'll need a window.onload event.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
Comment Utility
here's a version with leading 0's

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        
    </head>
    <body>
        <div id="date"></div>
        <script>
            
           ( function writeDate(){
            var date    = new Date(),
                month   = date.getMonth() + 1, // 0-11,
                day     = date.getDate(),
                year    = date.getFullYear(),
                hour    = date.getHours(),
                minute  = date.getMinutes();
            
                year = year.toString().slice(2,4);
                
                
                month = month < 10 ? "0"+month : month;
                minute = minute < 10 ? "0"+minute : minute;
            
                var dateContainer = document.getElementById("date");
                dateContainer.innerHTML = day+"/"+month+"/"+year+" "+hour+":"+minute;
            })();
            </script>
    </body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:JoeBo747
Comment Utility
Sorry I have not abandoned this post I have been travelling and will catch up tomorrow.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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 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)
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now