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
Related
The daterangepicker plugin allows to set date using the setStartDate and setEndDate methods.Is there any method to set the time in the plugin.
There doesn't appear to a method to set only the time, and they don't mention it in the docs, but if you can set it using the setStartDate and setEndDate methods.
Those methods will accept a moment object, and if you are only setting date values when you call those methods, the time will default to 12:00 am. Setting a time on the moment object before passing it in will update the time in the daterangepicker. Their docs don't mention it, but the picker will update the time as well as the date if you are using the timePicker. This will also work when initializing the picker for the startDate and endDate values, as well as limiting the times for minDate and maxDate.
Note that if you set a maxDate without a defined time, it will default to 12:00 am which can cause issues if you're using the timePicker. For example, if I set a maxDate of 04/04/2017, the picker will let me choose that date, but it will not let me select a time on 04/04/2017 past 12:00am. To fix this, I used the moment library to set .hour(23) and .minutes(59) on the moment object I wanted to set the maxDate to. This allows users to select any hour or minute on 04/04/2017. The docs for setting times on moments can be found here: https://momentjs.com/docs/#/get-set/
It's not exactly what you asked but I'll provide the solution I used to get around a similar issue for changing values that don't have methods here in case anyone else needs it. I needed to dynamically change the maxDate, but there isn't a built in method to change it, only an option to set it when initializing the picker. In order to work around it, I removed the date range picker and reinitialized it on the same input with the updated options. If you need to remove the picker, the use:
$('#myInput').data('daterangepicker').remove();
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);
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 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);