Flex- Time Zone Conversion - apache-flex

How to convert date and time to CDT time Zone in flex4
Regards,
Sushma

The Date object in Flash is always set to the computer's time settings. If the computer is already in the CDT timezone, then just getting any property from the object will be good. However, if you want to do a timezone 'conversion' into a timezone that the computer is not set to, you can get the UTC time and offset it like this:
var date:Date = new Date();
var timezone:int = -5;
date.hours = date.hoursUTC + timezone;
However, you're trying to get the actual CDT time, which only works during the summer in certain areas. For this, it's impossible for Flash to know exactly when that is UNLESS you code the exceptions (ie. if between this date and that date, do -6, else do -5) and you also need to know the actual location of the user (which is impossible through Flash unless the user gives you that info).
Can I ask why you need to know such a thing?

Related

Moment return the same value passed to it instead of utc value

Im trying to trasform a date to utc but moment return the same value i use.
for example
moment(date, 'YYYY-MM-DD HH:mm:ss').utc().format('YYYY-MM-DD HH:mm:ss')
if i use date = '2022-01-07 11:30:00' moment return 2022-01-07 11:30:00
do i have to set the timezone of the value first? why moment return the wrong value? it should return +3 hours that date.
You'll need to define the timezone in which the date is, then the offset will be as expected:
Example, using Europe/Amsterdam as timezone
const date = '2022-01-07 11:30:00';
const utc = moment(date, 'YYYY-MM-DD HH:mm:ss')
.tz('Europe/Amsterdam')
.utc()
.format('YYYY-MM-DD HH:mm:ss');
console.log(utc);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data.min.js"></script>
This will output 2022-01-07 10:30:00 since Amsterdam time is -1 compared to UTC.
Small side node, quoting MomentJS Project Status page
We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
In practice, this means:
We will not be adding new features or capabilities.
We will not be changing Moment's API to be immutable.
We will not be addressing tree shaking or bundle size issues.
We will not be making any major changes (no version 3).
We may choose to not fix bugs or behavioral quirks, especially if they are long-standing known issues.
The data you pass in doesn't have any indication of the timezone it's in, so moment is (I believe) assuming it's in utc already.
In related news, look into using the date-fns library instead of moment. Moment is getting old...
Moment github
Moment.js is a legacy project, now in maintenance mode. In most cases,
you should choose a different library.
This returns the same date since you never indicated any timezone
var time = moment("2013-08-26 16:55:00") //this creates time in my tz
You can set a timezone like this:
var time = moment("2013-08-26 16:55:00").tz("America/Los_Angeles");

moment.format() returns next day

So Im using Fullcalendar and on dayClick I'm trying to find events that match the day clicked.
var events = $calendar.fullCalendar('clientEvents');
if(events.length > 0) {
for (var i = 0; i < events.length; i++) {
if (date.format('YYYY-MM-DD') == events[i].start.format('YYYY-MM-DD')) {
...
Now
date.format('YYYY-MM-DD')
returns the day I clicked but when the for loop gets to that days events then
events[i].start.format('YYYY-MM-DD')
returns the next day. This seems to be affected by UTC but this applies to ALL days with events. Not just today. I've tried different formats and still the same. Here's something I noticed though:
n
_ambigTime:false
_ambigZone:true
_d:Thu Feb 09 2017 08:00:00 GMT-0500 (EST)
_f:"YYYY-MM-DD HH:mm:ss"
_fullCalendar:true
_i:"2017-02-08 13:00:00"
_isAMomentObject:true
_isUTC:true
_isValid:true
_locale:f
Notice that _d and _i are different. _i is actually the right date/time. So how can I reference that?
All moment properties starting with _ (like _d and _i) are for internal use and should not be used.
If you want to check if two moment object represent the same day you can use isSame passing the second parameter to limit granularity, instead of comparing formatted strings. In your case:
date.isSame(events[i].start, 'day')
The problem is that some of your objects are created in UTC mode (_isUTC: true), so they will be displayed using UTC time (previous day in some cases) instead of local time.
More info about UTC mode here:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.

What is the best way to get the date and time in server side?

I'm developing an asp.net mvc3 project. I have a trouble in this problem that I encounter. I will give a scenario so that it will understand well.
Scenario:
I have 2 PC (PC1(server) and PC2(client)). For example both two PC has different date and time let say for PC1 is +8GMT Date 8/10/2016 and for PC2 +8GMT Date 8/9/2016. I am using the PC2 the client and i'm using a code for getting the time is DateTime.Now(); in my controller and the time is display in label in one of my views. I tried to adjust the Date and Time of the PC2 the label for displaying t he time also change. What I want is even I change the Date and Time in PC2 it won't affect/change the displayed Date and Time in my label it will stick on what the Date and Time in the PC1.
This scenario is i'm using/testing the publish project
Any suggestions are welcome.
I'm not sure if I understand the question, but you might consider looking into the DateTime.ToUniversalTime method, which converts the value of the DateTime object to UTC.
This way, you may be able to work with a standard time from which you can convert to any time zone you might want to use to display in your application, regardless of server location.
DateTime serverTime = DateTime.Now;
DateTime utcTime = serverTime.ToUniversalTime;
string timeZoneId = "some time zone id";
TimeZoneInfo myTime = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
DateTime label = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myTime);
I am not very clear on your description. It seems as if you want your label to always show the time on the server (PC1) when the web page is loaded on the client by calling the site on the server, maybe like https://pc1. What you're doing should accomplish that: the time displayed in your label will be the system time from the server. Changing the time on the client will not affect it.
If you want your client (PC2) to show its local time, you will need to use code that runs on the client, i.e. JavaScript in most cases.
Working with dates in Javascript can be a little different from other languages/expectations, so I suggest reading the docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date.
Get started with var currentDate = new Date();.

Google Calendar API v3 time zone issues

Using .Net API wrapper for Google Calendar API.
First get primary calendar id
Get timezone of primary calendar (returns good data, e.g., "America/Los_Angeles")
Create a calendar event. Set start time and end time. Set timezone.
Dim eStart As New EventDateTime
eStart.DateTime = _startAt
eStart.TimeZone = GoogleTimeZone
Dim eEnd As New EventDateTime
eEnd.DateTime = _endAt
entry.Start = eStart
entry.End = eEnd
eEnd.TimeZone = GoogleTimeZone
CalService.Events.Insert(entry, calendarid).Execute()
But the events are being created at 3am when the start time specified is 11am.
Google API documentation states "A time zone offset is required unless a time zone is explicitly specified in timeZone" and for timezone "The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)".
Timezone value is being specified properly.
Basically, it is not making any difference whether or not timezone is specified. Event is created in GMT in google calendar.
What is wrong here?
Fixed it (or let's just say hacked it). Google .Net API wrappers are absolutely crap (and this goes to wrapper of all of their APIs and not just Calendar API).
The issue was that event.Start and event.End automatically converts dates and add a "Z" at the end. This tells Google that the date is in GMT format. There is no reason for putting a "Z" because even without it, Google considers GMT. So basically, event.TimeZone=value was being disregarded because the time was appended by "Z".
After I removed the "Z", everything worked ok.
entry.Start.DateTimeRaw = replace(entry.Start.DateTimeRaw,"Z","")
entry.End.DateTimeRaw = replace(entry.End.DateTimeRaw,"Z","")
I fixed it by creating an instance of a DateTime object that uses the DateTimeKind enum as one of the constructors. I found the default DateTime.Kind property value is DateTimeKind.Utc when deserializing a JSON date. That's why a Z (UTC) value is in the Raw for me. The time zone value will be correct when DateTimeKind.Local is applied to the DateTimeKind argument in one of the constructors that takes it.
DateTime dt = new DateTime(oldDateTime.Ticks, DateTimeKind.Local);
DateTime dt = new DateTime(yearVar, monthVar, dayVar, hourVar, minuteVar, secondVar, DateTimeKind.Local);
Instead of setting the Datetime property that is part of the Start and End objects, you should give the DateTimeRaw a value and assign a timezone to it like this:
eventItem.Start = new EventDateTime()
{
DateTimeRaw = input.Start.ToString("yyyy-MM-ddTHH:mm:ss"),
TimeZone = "America/New_York"
};
Noticed that I'm not adding a Z or any timezone representation at the end of the string format. That should fix the issue and should prevent google from ignoring the timezone property when you've set a value.
In addition, if you hover over the DateTime property of the Start or End object in Visual Studio, it is described as the following: DateTime representation of EventDateTime.DateTimeRaw(see image).
Which in this case, replacing the Z value for an empty string will only make the problem worst because your DateTime property will also be updated. I hope this is helpful to anyone in the future.

How do I let moment know the date that I'm giving it is in UTC?

Apologies in advance. Due to the popularity of this library I'm sure the answer is out there but I haven't been able to find it.
I'm getting timestamps back from an api in this format
2014-09-27T00:00:00-07:00
where this part is in UTC
2014-09-27T00:00:00
and this part is the timezone offset
-07:00
When converting to a moment I can't figure out how to let moment.js know that the time I'm passing it is in UTC.
If I try this
var time = moment('2014-09-27T00:00:00-07:00');
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));
time.local();
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));
both console logs output the same
however if I do this
var time = moment('2014-09-27T00:00:00-07:00');
time.utc();
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));
time.local();
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));
It will accurately add 7 hours (my time difference from UTC) log it, remove the time again and log it again.
I've tried
var time = moment.utc('2014-09-27T00:00:00-07:00');
but even then the .local() method doesn't do anything.
What am I missing?
An additional related question... In standard ISO 8601 format is the time string understood to be local time or UTC? With the offset one could convert in either direction, I'm just not sure what the starting point is.

Resources