There must be a simple way to get different timezones with code (ie without changeing your system timezone)
So far you can do something like
var timezone = TimeZone.CurrentTimeZone;
but I cant see any other way to get a different timezone?
or should I just use TimeSpan?
Actually it seems like it s a better idea to use TimeZoneInfo
Related answer
Creating a DateTime in a specific Time Zone in c# fx 3.5
how about using TimeZoneInfo.GetSystemTimeZones ?
This should return an array of all available TimeZone objects in this system.
Related
I am building a symfony2 app where the user can choose her timezone. I made the neseccary model/form changes to store a timezone field in the User object.
To apply the timezone inside a specific controller action I can just use:
$user = $this->get('security.context')->getToken()->getUser();
date_default_timezone_set( $user->getTimezone() );
Is there a way to do this without having to modify every controller/action?
This is an old question but the same answer still applies.
date_default_timezone_set should not be used in this manner. If you create a Datetime instance, the timezone will always be set to symfonys default timezone. This will lead to inconsistencies, I wish you the best of luck finding them :)
Basically, you should create a custom DateTime type that handles conversion of timezones. Additionally, if you really want different timezone on your server, set the timezone in config/parameters.yml so php handles all dates in the timezone you need.
parameters:
default_timezone: Europe/Warsaw
There's a nice tutorial here that will show you everything you need to know.
I have a byte[] and wanna convert it to object parameter in asp.net.
byte[] version =98471574580;
and I have One method that wants "ObjectParametere version".I must convert byte[] version to
ObjectParametere.
The way to do this has a few steps that if you follow in future shoudl save you needing to ask this sort of question again.
1) Look at the documentation for the class you want to use. It is found here: http://msdn.microsoft.com/en-us/library/system.data.objects.objectparameter(v=vs.110).aspx googleing for "ObjectParameter" returns it as top hit for me. Google might have noticed me doing a lot of MSDN searches though and bumping them to the top so if in doubt try adding MSDN or c# to the search.
2) Look for how to create an instance of the object. This would usually be through constructors. Sometimes you may use static methods but constructors are usual and in fact there are two here we can consider.
3) Decide which constructor to use. One takes a name and a type. One takes a name and a value. Since we have a value this seems like the one to choose.
4) Call the constructor you have decided to use and assign it to a variable (or use it directly in your code). In this case the syntax would be var objParam = new ObjectParameter("version", version);.
Some of these steps might be short circuited. For example if you are programming in Visual Studio then typing new ObjectParameter( should then offer intellisense help on what constructors are available, saving the need to look the documentation up online.
I have a situation which keeps coming up all the time when I think it's over. It's a ASP.NET project but I am not using ASP.NET AJAX elemets and any server side elements in design part. I am using ASP.NET to handle database operations and using jQuery elements in design, also using only jQuery AJAX to send and receive data from UI.
I have a method which runs when an ajax call is made and returns an object class with a DateTime element in it. I need to use what returns in a javascript function to show on the screen. I don't want to use JSON.NET or anything else to serialize the returning class beacuse ASP.NET does all the conversions when I return an object class, itself already.
So, my problem is I am getting a long date format which is not compatible with any javascript function and none of moment.js or date.js libraries supporting that format. The output of the datetime asp.net object in javascript is like:
"Sun Aug 11 2013 00:00:00 GMT +0300 (Turkey Daylight Time)"
I looked for some info about that for days and didn't find any questions or articles explaining how to cast returning datetime properly. May be not looking for the right thing...
I know that I can create a JSON string and send that class' values with it. I've been there. But my head starts hurting when I start to question the logic of asp.net automatically serializing returning classes and returning a non useful datetime object.
So, I decided to hear your opinions on that matter. What is the best way of dealing datetime conversion issues in asp.net?
I can change the DateTime object to a simple string and get the value as string and cast it to datetime in asp.net and return the same class with a string like a date object, but that is creating other casting problems in javascript side (DD/MM/YYYY and MM/DD/YYYY issues).
I can look for a way to properly cast the returning datetime format of asp.net to javascript form (which I believe is after a lot of research is quite impossible)
I can create my class which would include a long int object and cast (serialize) that datetime object to JSON format before sending as a return call in asp.net method.
Which one of these is better and why, and is there another, better way to do this?
Thanks...
In my play framework app the user has the choice to enter a date for a schedule, the date is then mapped to my model entity:
#InFuture
#As("dd/MM/yyyy HH:mm")
public Date validFrom;
This is the field in the form
<input type="text" name="schedule.validFrom" placeholder="dd/mm/yyyy hh:mm">
The problem is that our server is running in a non-local time zone and the timezone should be taken from the object where this schedule is being made for.
So I know upfront what the timezone is for this schedule and I don't want the user having to enter the timezone in the field.
A possible solution would be to submit the date as string and do manual validation and parsing but I wonder if there is a better solution.
Manual parsing is probably your best option.
You could possibly create your own object containing the date and the timezone.
Then implement your own validation check based on InFutureCheck and perform the timezone conversion.
The better option will be to give user a drop down list of timezones. For example:
String TIMEZONE_ID_PREFIXES ="^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";
String[] ids = TimeZone.getAvailableIDs();
Timezone tz = null;
for (int i=0; i<ids.length; i++) {
if (ids[i].matches(TIMEZONE_ID_PREFIXES)) {
//add TimeZone.getTimeZone(ids[i]).getDisplayName() to a drop down list
}
}
The best practice here really depends on whether your users need to see multiple timezones.
If they do not need multiple timezones, then you can assume all times are 'zone-less' aka they are all UTC or GMT. This is by far the simplest option.
If users need to see multiple timezones, then you are best off passing the timezone with each date with the Z parameter: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#rfc822timezone Meanwhile, in Javascript, you will probably want to use a library to create a date string with a timezone, such as: http://arshaw.com/xdate/#Formatting
i've been using structuremap since a couple of months. I always use ObjectFactory.GetInstance to take the right instance of the object i've to use.
Actually, i need to understand which is the default ObjectFactory's InstanceScope. Is it ThreadLocal?
do u know where i can read about it?
first result on google for "structuremap lifecycle":
http://structuremap.github.com/structuremap/Scoping.htm
"PerRequest" is the default lifecycle if you didnt specify one in your registry