Display and format a UTC date to my timezone - javascript

I have a string representing a UTC date:
'2016-07-29T01:33:56.72'
I would like to display this date corrected to my timezone and nicely formatted:
'2016-07-28 22:33:56'
I tried libraries like moment.js or numeral.js but I still didn't found a clear way to do it. My problem so far is that I am starting with a string object and not a Date object.

Answered here for moment.js: How to convert Moment.js date to users local timezone?… – Voyta

Related

convert date time with timezone to UTC in javascript

I have a date time string like this 2018-09-10T12:05:00 and I know the timezone associated with the date. Assume timezone available is Asia/Singapore. How can I get the UTC date with this info available?
There's no timezone associated with date string itself. First step would be to associated a timezone with date and then convert it into UTC. Suggested answers are close but do not help my case ?
UPDATE:
Still no luck with this.
Simply just use this method:
var isoDate = new Date('yourdatehere').toISOString();

moment js, converting EST string to UTC

I'm a new developer and I have been stuck on this for a while, how would I convert the following date to it's UTC version (no utc prefix/suffix needed) in moment JS?
05/03/2016 16:00
I tried:
var b = "05/03/2016 16:00";
moment.utc(b).format();
"2016-05-03T16:00:00Z"
I would like the final result to be exactly as b, but its UTC equivalent. What am I missing?
You're missing the input format and the output format.
Without an input format, your date could be interpreted as either May 3rd, or March 5th, depending on the culture.
Without an output format, moment has no way to know what kind of string you want, so it defaults to the full ISO8601 extended format.
You're also missing the conversion. If the input is local time and you want UTC, then you should parse it with moment(...) then convert it to UTC with the .utc() function.
In summary:
moment(b, 'MM/DD/YYYY HH:mm').utc().format('MM/DD/YYYY HH:mm')
Also note that it's converting from the local time where the code is running. in your question you asked about EST. If your local time zone is something else, then EST will not apply to the above. You would instead need to use the moment-timezone add-on to do the following:
moment(b, 'MM/DD/YYYY HH:mm', 'America/New_York').utc().format('MM/DD/YYYY HH:mm')
moment(b).utc();
Will change it to a utc time, you might still need to add parameters to format() to make sure it displays the way you originally wrote it.
moment(b).utc().format("MM/DD/YYYY HH:mm");

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

Convert date string to date object using moment

there is a situation which I need to convert a date string which consist a timezone abbrevation to a Moment object and parse it. How can I do this? sample date is like below:
var dateString = "2015-01-14 06:57:47 ECT";
be aware that I need the timezone of this date, because Im going to do another conversion to another zone, so if we just consider the date we miss the accuracy.
I don't think moment can handle a timezone name, you should replace ECT with the offset value, like +0200

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