convert time of certain timezone to UTC using momemtjs - javascript

I have a time in a specific timezone , I want to covert it to UTC . how can I achieve that using moment timezone ?
http://momentjs.com/timezone/
in the documentation this is how to convert :
jun.tz('America/Los_Angeles').format('ha z');
I am just not sure what timezone name to pass to convert it to UTC, or is there another function to use?

You can easily construct a moment in a specific time zone by using the moment.tz(...) syntax. This is slightly different from doing conversions with the .tz(...) function of an existing moment object, which is what you showed in your question.
var m = moment.tz('2016-03-25 08:00:00', 'America/Los_Angeles')
Once you have a moment object, you can convert it to UTC by calling the .utc() function. You can then format it however you like.
moment.tz('2016-03-25 12:34:56', 'America/Los_Angeles').utc().format()
// output: "2016-03-25T19:34:56+00:00"

Related

constructing a date object along with time zone

I want to construct a Date object along with dynamically selected timezone. I am currently in IST time zone. I want to eliminate the usage of Date.parse() as it does not behave as expected at times.
let's assume tzOffset to be +05:30 for now. It could be any other timezone based on what users want. new Date(epochDate).toISOString(); converts the date to UTC timezone. How do I get the date in toISOString() format but also get it in the desired time zone
const tsConstruct = `${year}-${month}-${date}T${hour}:${min}:00${tzOffset}`;
const epochDate = Date.parse(tsConstruct);
scheduledTs = new Date(epochDate).toISOString();
JavaScript's Date does not store timezone info. It just stores the number of milliseconds from UNIX EPOCH. Then, depending if you use the UTC methods or not, it returns the date and time in UTC or the local time.
You should have to change the date and time, based on the timezone indicated, to UTC or local time, and then store it in a Date object. But, of course, to show the stored time in another timezone different to the local one or UTC, you must do the conversions yourself, so, as #RuChengChong suggested, use a helper library like momentjs.

How to convert "2017-07-27T08:02:17+0200" to local date-time and zone using moment.js

How to convert "2017-07-27T08:02:17+0200" to local date-time and zone using moment.js?
Here 08:02:17 is hour:minute:second and +0200 is time-zone. My local time-zone is GMT+6. I want to convert that date as my local date-time and zone. I've tried this so far:
moment.utc('2017-07-27T08:02:17+0200','YYYY-MM-DDThh:mm:ssZZ').local()
But it is returning Invalid Date by moment.js
As stated here
By default, moment parses and displays in local time.
Your input string includes UTC offset, so you can simply use moment(String, String).
Note that as stated here:
Moment's string parsing functions like moment(string) and moment.utc(string) accept offset information if provided, but convert the resulting Moment object to local or UTC time.
so there is no need to use local().
var m = moment('2017-07-27T08:02:17+0200', 'YYYY-MM-DDTHH:mm:ssZZ')
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
For the local the moment itself will do, but, if you want a specific timezone, you can use the tz method with the location name (as defined in Moment Timezone):
moment.tz("2017-07-27T08:02:17+0200", "America/Toronto").format();

momentjs toDate() date return not same as format() date

I am using moment 2.16.0 and want starting days of month. There are different result of toDate() and format() method. Here is jsfiddle.
code:-
var time=moment().subtract(0,'months').startOf("month").format();
console.log(time); //2016-12-01T00:00:00+05:30
var time2=moment().subtract(0, 'months').endOf("month").format();
console.log(time2); //2016-12-31T23:59:59+05:30
var time=moment().subtract(0,'months').startOf("month").toISOString();
console.log(time); //2016-11-30T18:30:00.000Z here i want somethings like 2016-12-01T00:00:00.000Z
var time2=moment().subtract(0, 'months').endOf("month").toISOString();
console.log(time2); // 2016-12-31T18:29:59.999Z here i want somethings like 2016-12-31T59:59:59.000Z
All of your operations are using moment with local time except the toISOString, which will give you the string in UTC. Since your timezone is offset from UTC, naturally the local time string (from format) and the UTC time string (from toISOString) are very different.
here i want somethings like 2016-12-01T00:00:00.000Z
That would be a different time from what that Moment instance represents.
If you want something in an ISO-8601 format but in local time, you can use format with the appropriate set of formatting tokens, but you don't wan the Z at the end because, again, you're not dealing with UTC ("Zulu") time, you're dealing with local time.
moment().format("YYYY-MM-DDThh:mm:ss.SSS")

Convert string to new Date object in UTC timeZONE

Can anyone let me know how to convert a string to a date Object with UTC time zone in ExtJs?
String is "2015-10-07T23:59:00". I would like to get the same in Date Object without changing the timezone.
First of all, your date string does not have a timezone.
When you make a JavaScript date object from a string, there are two possible outcomes you could expect:
You may want the date to be 23:59 Local (23:59 CEST in my case).
In this case, you want to use new Date("2015-10-07 23:59:00") with plain javascript (note the missing T), or Ext.Date.parse("2015-10-07T23:59:00","c");.
You may want the date to be 23:59 UTC (e.g. 01:59 CEST).
In this case, you want to use new Date("2015-10-07T23:59:00").
Of course, whenever you output the date, you have to get the date in the correct time zone as well. The console/toString will usually show it in local time. JavaScript does provide getUTC... methods if you require other time zones.
You see, using Time Zones with JavaScript is a painful experience. I would recommend to try moment.js if you need full time zone support.
You can use Ext.Date.parse.It gives Date Object as output.It syntax is:
Ext.Date.parse( String input, String format, [Boolean strict] )
For Example:
Ext.Date.parse("2015-10-07T23:59:00", "Y-m-dTH:i:s");
try
var millisFromEpoch = Date.parse('2015-10-07T23:59:00');
it will parse date in GMT timezone, Ext.date.parse use the current timezone instead

Given a date/time and timezone, how do you convert that into a UTC timestamp?

I am calling an API that returns a date/time string such as "2014-04-30 15:32:01". On top of this, I have a known timezone that this date/time exists in. I can see from the javascript Date() class has a .UTC() call for this, but that does not seem to accept a timezone as far as I can tell.
Given the date/time string + timezone, how can I convert those into a UTC timestamp?
I'd recommend using Moment.js and Moment Timezone.
You can create a timestamp using Date.parse from your local time (RFC2822 date format to include the timezone), create a date from that and use Date.toUTCString to get the UTC time. Not sure if it'll work with day light savings though.
Example: http://jsfiddle.net/3vXV6/ (will alert the date)
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString

Categories