Hi I am using Razor as template for my .net applications.
Currently I am displaying time by #DateTime.Now.ToString("f").
Please help me how can I display date time specific to end user's zone in razor
use this keyword
local time depends on where the computer is located. The universal time is independent of this
#DateTime.UtcNow;
TimeZone zone = TimeZone.CurrentTimeZone;
// Demonstrate ToLocalTime and ToUniversalTime.
DateTime local = zone.ToLocalTime(DateTime.Now);
DateTime universal = zone.ToUniversalTime(DateTime.Now);
Related
Do you know of any way to check the current time in Joomla considering the selected time zone in global configuration?
I have been looking in administrator settings but did not see any single word about the current time.
You can use the following code:
$date = new DateTime();
$config = JFactory::getConfig();
$date->setTimezone(new DateTimeZone($config->get('offset')));
This does two things. The first line creates a DateTimeobject based on the current server time and timezone. The following two lines get Joomla's global configuration and change the timezone of out date object to that timezone.
You can then format the date to whatever format you like by calling $date->format('Y-m-d H:i:s');. You can replace the format specifier by whatever you need, a reference of possible formatting options can be found here: http://www.php.net/manual/de/function.date.php
I have the Date/Time stored in the database on the London's time zone (not UTC).
What I need is to retrieve this Date/Time, convert it to UTC and display considering the user's time zone (which they define when registering to the site - ie: en-GB, en-US, etc).
My first question is more related to the MVC structure, as I'm totally new to this (am a WebForms developer) - Where should I put this conversion code/class/helper? Model? ViewModel?
The second question is the conversion itself - I've played around with the NodaTime test project but can't find a proper way to do this conversion.
Any help is much appreciated.
Thank you in advance.
Jon Skeet may need to correct me, as I'm assuming this is correct in nodatime:
// Timezone data provider (inject with DI)
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
// London Timezone (can keep this as a singleton, since it'll be used often)
var londonTimeZone = timeZoneProvider["Europe/London"];
// Get your date/time from the database and map it as being local to London timezone
var yourDateFromDb = new DateTime(2013, 01, 23, 21, 00, 00); // This is what you'll get back from your database (although you may get back a DateTimeOffset)
ZoneLocalMapping zonedDbDateTime = londonTimeZone.AtLeniently(LocalDateTime.FromDateTime(yourDateFromDb)); <-- This is your date time with the correct offset (taking into account DST etc.)
// Map the London zoned date/time to the users local date/time
var usersTimezoneId = "Europe/Paris"; // <-- Store this value in users profile/db
var usersTimezone = timeZoneProvider[usersTimezoneId];
var usersZonedDateTime = zonedDbDateTime.WithZone(usersTimezone);
Assert.That(usersZonedDateTime.Hour == 22);
You should probably be aware that during timezone transitions (autumn clock change), you may get 2 possible dates for the zonedDbDateTime. The code here just gets the first or earliest date/time.
Edit: Updated code snippet with changes suggested by Jon Skeet
you can write some extension method like this :
public static DateTime ConvertToUtc(this DateTime d)
{
//do conversin and return it
}
and you will be able to call it like #Model.MyDate.ConvertToUtc() from any view you wish as long as you have the namespace of the ConvertToUtc inclucded on the top of page.
and to do the conversion see this pages
Convert DateTime from UTC to a user provided time zone with C#
http://www.dotnetcurry.com/ShowArticle.aspx?ID=593
How to work with time zones in ASP.NET?
I have MVC3 project with Entity.
I have Data Annotation on DateTime property with DisplayFormat(DataFormatString="{0:dd/MM/yyyy hh:mm:ss}")
Controller with method which has entity as argument and calling view with same entity object, which displaying its date property.
So, I open browser and enter link http://site/entity/method?dateproperty=01/09/2012 01:02:03 (1 September 2012) and it displays 09/01/2012 01:02:03 (9 January 2012).
In view I'm using DisplayFor it's displaying like dd/MM/yyyy as I have a set DisplayFormat.
Problem is that it's reading like MM/dd/yyyy instead of dd/MM/yyyy, any solution?
P.S. I have tried globalization in web.config <sysmte.web> but didn't help.
I suspect the problem is that you haven't specified ApplyFormatInEditMode = true in your attribute, so it's parsing using the default format, but formatting with the format you're specifying.
I'm not an MVC dev by any means, but if you want the same format to be used in both directions, I believe you can just change your attribute to:
[DisplayFormat(ApplyFormatInEditMode = true,
DataFormatString="{0:dd/MM/yyyy HH:mm:ss}")]
Note that I've changed hh to HH, as I suspect you want a 24-hour format. I'd actually suggest using yyyy-MM-ddTHH:mm:ss (ISO-8601) as a less ambiguous format for the URL parameter - with a more user-centric display format, of course.
I am trying to update the date in a column based on the information contained in another column. I know it will be an "IF" function, but I am not getting the formula correct.
I need to update the due date column by adding the time based from the frequency columnn(week, day, month, etc...) to the completion date column....please help!
I would not use the Designer inside Visual Studio to handle an If-Clause. Reason: I didn't figure out how to use this and from other things lieke the While-Activity I read that this is much slower than a coded one.
For some handsome looking I would add "CodeActivity" and link/ invoke my method to this. Inside this method you could use
string frequency = workflowProperties.Item["name of other column"].ToString();
string oldDate = workflowProperties.Item["name of updating column"].ToString();
DateTime newDate = Convert.ToDateTime(oldDate);
if (String.Equals(frequency, "weekly", StringComparison.OrdinalIgnoreCase))
{
<your code like:> workflowProperties.Item["name of updating column"] = newDate.AddDays(7);
}
else if (...)
or use switch (frequency){}
Remind that workflowProperties.Item refers everytime to selected Item. Because this is a global variable inside your workflow, you can access to it from every method. If you don't understand this example, be free to ask.
Shegit
How to manage timezone in magento at the time of product creation, in frontend i am filtering products by time,but problem is that client wants to show products creation time depend on user's timezone ? for example i am adding a product in admin area it shows 12.21pm but at the same time customer viewing this product from other country it should displays current time according to user's time zone, how can we do that?
please help me to get out of this.
Thanks in advance.
Be more specific about how you are generating the date and how you wish it to be displayed...
Here is a general approach:
In your template code place a span with a class around your time values. These time strings can be formatted with lots of options including the timezone in your php code.
Place javascript where appropriate: either in the header (onload function), in the footer or inline with your view.phtml.
In your javascript use the Prototype library to iterate over the time spans, e.g.:
$$('yourdateclass').each(function() {
//
});
On each of those elements convert the time with Date.parse() to a date object. Then use getTimezoneOffset to account for the visitors location, add/subtract accordingly and then update the element inner html to your preferred way of showing the time/date.
It worked for me.
$session = Mage::getSingleton('customer/session');
$time_zone = $session->getCustomer()->getTimezone();
$timeoffset = Mage::getModel('core/date')->calculateOffset($time_zone);
$timeoffset = $timeoffset - Mage::getModel('core/date')->getGmtOffset($time_zone);