6

Is there any simple way to convert the following:

2011-08-31T20:01:32.000Z

In to UK date format: 31-08-2011

and time to: 20:01

7 Answers 7

13

You can use momentjs (http://momentjs.com/):

var date = moment(dateObject).format("YYYY-MM-DD");
var time = moment(dateObject).format("HH:mm:ss"); 
Sign up to request clarification or add additional context in comments.

Comments

4

You can use jquery-dateFormat plugin. The following should do the trick:

$.format.date('2011-08-31T20:01:32.000Z', "dd-MM-yyyy"));
$.format.date('2011-08-31T20:01:32.000Z', "hh:mm"));

Comments

4

Date:

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var date = currentTime.getDate();
var year = currentTime.getFullYear();
$('#date1').html(date + '-' + month + '-' + year);

Time:

<script type="text/javascript">
  var tick;

  function stop() {
    clearTimeout(tick);
  }

  function clock() {
    var ut=new Date();
    var h,m,s;
    var time="";
    h=ut.getHours();
    m=ut.getMinutes();
    s=ut.getSeconds();
    if(s<=9) s="0"+s;
    if(m<=9) m="0"+m;
    if(h<=9) h="0"+h;
    time+=h+":"+m+":"+s;
    document.getElementById('clock').innerHTML=time;
    tick=setTimeout("clock()",1000); 
  }
</script>
<body onload="clock();" onunload="stop();"> 
  <p><span id="clock"></span></p>
</body>

Comments

2
var a = '2011-08-31T20:01:32.000Z';
var b = new Date(a);

See http://www.w3schools.com/jsref/jsref_obj_date.asp for methods you can use on b now.

1 Comment

on IE8: new Date('2011-08-31T20:01:32.000Z') does not works. It's return NaN. Fix is requiered to do this in IE8 and older.
2
var rg=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\..*/g;
var dateStr="2011-08-31T20:01:32.000Z".replace(rg,"$3-$2-$1"); // result is 31-08-2011
var timeStr="2011-08-31T20:01:32.000Z".replace(rg,"$4:$5"); // result is 20:01

Comments

1
var date = new Date("2011-08-31T20:01:32.000Z").toLocaleDateString();
// date = 2011/08/31

date = date.split("/");
// date = ["31", "08, "2011"]    

date = date[2] + "-" + (date[0].length == 1 ? "0" + date[0] : date[0]) + "-" + (date[1].length == 1 ? "0" + date[1] : date[1]);
// data = 2011-31-08

$("#your-txtbox").val(date);

Comments

1

Use the date object:

d = new Date('2011-08-31T20:01:32.000Z');
date = d.format("dd-mm-yyyy");
time = d.format("HH:MM");

2 Comments

You are right, it's not. But if you follow the link you see a function extending Date with a format function. Sorry, I should have mention that.
on IE8: new Date('2011-08-31T20:01:32.000Z') does not works. It's return NaN. Fix is requiered to do this in IE8 and older.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.