How to convert 10:09:00 GMT + 0000 to IST in javascript?

I am developing a web application in which we use the CK Editor calendar. In which I execute the code below to get Date & Time ..

Code:

 var strDate = new Date(event.start); var endDate = new Date(event.end); var title='Event :'+event.title+' From :'+ event.start.toLocaleString() +'To:'+event.end.toLocaleString()+' By :'; 

Output:

 Fri Feb 13 2015 10:37:00 GMT+0000 To :Fri Feb 13 2015 10:37:00 GMT+0000 

Expected Conclusion:

In the above output, delete GMT + 0000. Replace the correct session, be it AM / PM .

+6
source share
2 answers

You have included jQuery with your tags, and so I assume that with the jQuery plugin you will be fine. jQuery-dateFormat is easy to use.

 var date = new Date(); var fDate = $.format.date(date, 'E MMM dd yyyy hh:mm:ss a'); $('#date').text(fDate); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://raw.githubusercontent.com/phstc/jquery-dateFormat/master/dist/jquery-dateFormat.min.js"></script> <div id="date"></div> 
+1
source

The next function should do what you want. It parses the string to create a date object using UTC values, then adjusts the UTC time to go to IST (+0530), and then returns a formatted string with equivalent IST values.

 // Expects string in format: Fri Feb 13 2015 10:37:00 GMT+0000 // Assumes is UTC // Returns values in IST function parseAndFormat(s) { // Helper function z(n){return (n<10? "0":'') + n;} // Split into parts var b = s.split(/[ :]/); var m = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; // Create a date based on UTC values var d = new Date(Date.UTC(b[3], m.indexOf(b[1]), b[2], b[4], b[5], b[6])); // Add 5 hours 30 minutes to the UTC time -> IST d.setUTCHours(d.getUTCHours() + 5, d.getUTCMinutes() + 30); // Format the hours for am/pm var hr = d.getUTCHours(); var ap = hr > 11? 'PM' : 'AM'; hr = hr%12 || 12; // Format the output based on the adjusted UTC time var dt = days[d.getUTCDay()] + ' ' + m[d.getUTCMonth()] + ' ' + d.getUTCDate() + ' ' + d.getUTCFullYear() + ' ' + z(hr) + ':' + z(d.getUTCMinutes()) + ':' + z(d.getUTCSeconds()) + ' ' + ap; return dt; } console.log(parseAndFormat('Fri Feb 13 2015 10:37:00 GMT+0000')) // Fri Feb 13 2015 04:07:00 PM console.log(parseAndFormat('Fri Feb 13 2015 20:37:00 GMT+0000')) // Sat Feb 14 2015 02:07:00 AM 

This method will work with local values, except when daylight saving time interferes. Please note that this is always an IST; it will not regulate daylight saving time observed in the places where it is used.

Edit

Fixed 12 hour conversion.

+2
source

Source: https://habr.com/ru/post/982415/


All Articles