-1

How do I convert datetime format into 2012-02-22T11:34:00.000+0000 ?

I wan to convert datetime format into this format as shown below :

year-month-dayT10:45:00.000+0000

Example :

2012-02-22T11:34:00.000+0000
user2226755
  • 12,494
  • 5
  • 50
  • 73
  • duplicate: http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – Ross Presser Jul 18 '14 at 05:20
  • Hi ross presser i am getting result as "18-7-2014" but i need to get result as "2012-02-22T11:34:00.000+0000" – PavanFoxPro Jul 18 '14 at 05:27

2 Answers2

0

By passing general date in this function yoou can do it.

function getDateFormat(generalDate){
var sapDate = '0000-00-00T00:00:00';
   var date;
   if (!generalDate) {
       //NOP
   } else if(typeof generalDate == "string"){
    var dateArray = generalDate.split("-");
    if(dateArray.length < 2)
     dateArray = generalDate.split("/");
   date = new Date(monthNumber[dateArray[1]]+"/"+dateArray[0]+"/"+dateArray[2]);
   } else {
       date = new Date((generalDate.getTime() - (new Date().getTimezoneOffset() * 60000)) + 19800000);
   }

   if (date) {
       var monthIn =null;
       if(date.getMonth() < 9)
           monthIn = "0"+(date.getMonth()+1);
       else
           monthIn = date.getMonth()+1;

       var date2 = date.getDate();
       if(date2 <= 9)
           date2 = "0"+date2;

       sapDate = date.getFullYear()+'-'+monthIn+'-'+date2+'T00:00:00.000+0000';
   }

console.log(sapDate);
}

The working fiddle is here

Roshan
  • 2,144
  • 2
  • 16
  • 28
  • can i use only javascript and run without using html tags in fiddle?? I am new to javascript and fuddle i am unable to use it?? will i need to write code in html to run and print ?? can u please help me out – PavanFoxPro Jul 18 '14 at 05:48
  • This is what i have done in my fiddle . click on the above link you will see that i have not used any html tag. It is only java script that i have used. – Roshan Jul 18 '14 at 05:53
  • Thanks for fast reply. U said "By passing general date in this function yoou can do it." i passed same but only in top of function in 1st line but after running i am getting blank screen nothing is displaying. Can u please say in what lines did i need to change "general date" and i can get the output. Even now i am getting blank screen – PavanFoxPro Jul 18 '14 at 05:57
  • I insist you to please see the fiddle link that i have posted. here as a parameter of getDateFormat function i have passed new Date() which contains today date and time. For your output see the result in console. – Roshan Jul 18 '14 at 06:25
0

Try this

<script>
var myDate = new Date();

var date = myDate.getDate();
var month = myDate.getMonth() + 1; 

var year = myDate.getFullYear();
var h = myDate.getHours();
var m = myDate.getMinutes();
var s = myDate.getSeconds();
var ms = myDate.getMilliseconds();
document.write(year + "-" + month + "-" + date + "T" + "-" + h + ":" + m + ":" + s + "." + ms);

</script>
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
jned29
  • 477
  • 12
  • 50