Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm Developing web app, In that We are using CK Editor Calendar. In which I'm execute below code for 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

Expect Output:

In the above output it should remove the GMT+0000, Replace the Proper session whether AM/PM.

like image 479
Sundar Avatar asked Feb 13 '15 05:02

Sundar


2 Answers

The following function should do what you want. It parses the string to create a date object using UTC values, then adjusts the UTC time to get to IST (+0530), 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 would work with local values except where daylight saving gets in the way. Note that this is always IST, it will not adjust for daylight saving observed in places that use it.

Edit

Fixed 12hr conversion.

like image 151
RobG Avatar answered Oct 06 '22 06:10

RobG


You included jQuery with your tags, and so I assume you'd be okay using a jQuery plugin. jQuery-dateFormat is simple 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>
like image 45
Bitwise Creative Avatar answered Oct 06 '22 06:10

Bitwise Creative