how to convert GMT time to ist time in javascript
You can use a built-in filter for dates:
var app = angular.module('myApp', []);app.controller('myCtrl', function($scope) { $scope.timestamp = new Date("2018-06-08T08:52:51.871Z");});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
GMT: {{timestamp | date: 'dd-MM-y h:mm a'}} <hr> IST: {{timestamp | date: 'dd-MM-y h:mm a' : '+0430'}}
</div>
Convert the time stamp UTC to IST using JavaScript
First thing, take a look at JavaScript date formats and convert your input date accordingly, then you shoud take a look to the methods available in JavaScript for date manipulation (no external library required). It's pretty easy to do something like this:
var dateUTC = new Date("yourInputDateInASuitableFormat");
var dateUTC = dateUTC.getTime()
var dateIST = new Date(dateUTC);
//date shifting for IST timezone (+5 hours and 30 minutes)
dateIST.setHours(dateIST.getHours() + 5);
dateIST.setMinutes(dateIST.getMinutes() + 30);
Convert UTC time into IST time in node js
You can use moment.js, as others have already suggested.
But in your question is not clear if you want the output as UTC or IST (the question title says "Convert UTC to IST", but in the question the desired output (2017-08-08 17:16:51
) is the corresponding UTC date/time).
Anyway, if you want the output in UTC, you can do this:
var utc = 1502212611;
var m = moment.unix(utc).utc().format('YYYY-MM-DD HH:mm:ss');
console.log(m);
The unix
method takes a value of seconds since epoch (1970-01-01T00:00Z
). Then the utc()
method makes sure the date will be formatted as UTC (if you don't call it, the date will be displayed using the default timezone, which can vary according to your system).
The output will be:
2017-08-08 17:16:51
If you need to convert to IST, you'll need to also use moment timezone:
var utc = 1502212611;
var m = moment.unix(utc).tz('Asia/Kolkata').format('YYYY-MM-DD HH:mm:ss');
console.log(m);
The tz
method converts the date to another timezone. The rest of the code is similar to the previous one.
The output will be:
2017-08-08 22:46:51
Note that I used Asia/Kolkata
instead of IST
. That's because the API uses IANA timezones names (always in the format Region/City
, like Asia/Kolkata
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like IST
or PST
) because they are ambiguous and not standard - IST
can be "India Standard Time", "Israel Standard Time" or "Irish Standard Time", and the API can't deal with such ambiguity (the code above won't work with IST
, so you must use a proper timezone name).
You can get a list of all available timezones names (and choose the one that fits best to your case) by calling moment.tz.names()
.
In plain Javascript, you must do it manually:
// pad zero if value <= 9
function pad(value) {
return value > 9 ? value: "0" + value;
}
var utc = 1502212611;
var d = new Date(0);
d.setUTCSeconds(utc);
var m = d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate())
+ ' ' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes())+ ':' + pad(d.getUTCSeconds());
console.log(m);
Output:
2017-08-08 17:16:51
How to convert GMT time to Local time?
You can get the localHour
using getTimezoneOffset()/60
. This is a very hacky way of doing it since you don't have the entire Date
object for GMT.
var dateTime = "10:18:00"; // GMTvar hour = dateTime.split(':')[0];var minute = dateTime.split(':')[1];
var date = new Date(); // UTCvar localHour = hour - date.getTimezoneOffset()/60;var localMinute = (localHour%1) * 60;
localHour = Math.floor(localHour);localMinute += parseInt(minute);if (localMinute >= 60) { localHour += Math.floor(localMinute/60); localMinute %= 60;}
localHour %= 24;
console.log('local hour:', localHour);console.log('local minute:', localMinute);
How to convert GMT time to IST?
You need to pass the timestamp:
date_default_timezone_set('Asia/Kolkata');
echo date('d M H:i', $strToDate); // where $strToDate is 1520489880
Would produce the following value:
08 Mar 11:48
How to convert a Date to UTC?
The
toISOString()
method returns a string in simplified extended ISO
format (ISO 8601), which is always 24 or 27 characters long
(YYYY-MM-DDTHH:mm:ss.sssZ
or±YYYYYY-MM-DDTHH:mm:ss.sssZ
,
respectively). The timezone is always zero UTC offset, as denoted by
the suffix "Z
".
Source: MDN web docs
The format you need is created with the .toISOString()
method. For older browsers (ie8 and under), which don't natively support this method, the shim can be found here:
This will give you the ability to do what you need:
var isoDateString = new Date().toISOString();
console.log(isoDateString);
Related Topics
Allowing/Replacing Comma as Number Separator for Input Type="Number" Using Rails
Check If Page Gets Reloaded or Refreshed in JavaScript
Javascript - Adding White Space to Left of String
How to Get Around .Push Is Not a Function in JavaScript
Check If Second String Is Rotation of Another String
Converting String Date in React/Javascript
React: Do Children Always Rerender When the Parent Component Rerenders
Append Data to Json Array Issue
Redirect Another Page After Successful Form Submission
How Display Selected Item from Bootstrap 4 Drop-Down Item Menu
How to Stop the Iframe from Constantly Reloading After Setinterval (Jquery)
React Native Init Android,Ios and App Folder Missing
How to Extract Data from Json Nested Objects
Javascript or Jquery Browser Back Button Click Detector