So in my project a user from USA set some time say it was 10:30 am
and now when a person some another country see that time then it should be according to their timezone .
For Example in USA it is 5:30 am now
and in india it is 6:30 pm , so if after 5 hours a person in india sees that then that person should see 6:30 pm for that post
Use php function gmdate()
string gmdate(string $format[,int $timestamp= time()] )
Identical to the date()function except that the time returned is Greenwich Mean Time (GMT).
Parameters format The format of the outputted date string. See the formatting options for the date() function. timestamp The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().Return Values Returns a formatted date string. If a non-numeric value is used for timestamp, FALSE is returned and an E_WARNING level error is emitted.
in timestamp add the interval to.
Ex if GMT zone is 5 say then add timestamp+5*60*60
Use below function:
class func getDateWithFormat(format: String) -> NSDate {
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = format
var newDate: NSDate = dateFormatter.dateFromString(dateFormatter.stringFromDate(NSDate()))
return newDate
}
Eg: var todaysDate: NSDate = NSDate.getDateWithFormat("dd:mm:yyyy hh:mm:ss a")
I created the below function to solve this problem.
How it works: suppose this was your local time converted to GMT zero - 2016-04-14 21:00:00 +0000
and when I convert it using following function, I'm here in New Delhi India, which has GMT+05:30, then I will get time 2:30 of morning.
+(NSString*)getLocalTimeFromGMTzero : (NSString*)GMTzerioTimeString {
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
//Create the date assuming the given string is in GMT
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [df dateFromString:GMTzerioTimeString];
//Create a date string in the local timezone
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [df stringFromDate:date];
NSLog(#"date = %#", localDateString);
return localDateString;
}
Related
I have this code to convert a string to date:
NSString* strToConvert;
NSDate* dtToReturn;
//...code to parse string
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy, HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
dtToReturn = [dateFormatter dateFromString:strToConvert];
I send it this string "08/30/16 02:22:00 PM"
and it returns this date: 2016-08-30 16:22:00 +0000
which is expected since I am +4 from GMT
But if I send it this string: "08/30/16 03:22:00 PM"
it returns the same date: 2016-08-30 16:22:00 +0000
I only pass one string at a time to the method. Am I doing something wrong in my dateformatter?
Because strToConvert uses AM/PM hour time and date formatter is using 24 hour time, this leads to a wrong date conversion. Fix date formatter's dateFormat to use AM/PM hour time: #"MM/dd/yy, hh:mm:ss a"
I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTCÂ (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.
Is it possible to convert input string with any format into date?
I want to receive ate and time from textfield with any format like date: may 22 2000, wed may 20, may, 30 2000 etc.. and also need to get correct value according to the local timezone.
please help?
NSDateFormatter can help convert any type of date NSString to NSDate. The important thing is to use the right format for the date.
Here is some code you can try
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
You will have to set the date format to whatever format your string is in. Here are some date format specifiers: (Complete list here)
eeee - Local day of week spelled out
yyyy - year (4 digits)
MMMM - Month spelled out
dd - day of month with no leading zeros
HH - hour of day (24 hour format)
mm - minutes of hour (with leading zero)
Edit
The date format for the following dates will be:
May 03 2000 : "MMM dd yyyy"
Mon, Jan 03 : "eee, MMM dd"
Mon 31 Jan : "eee dd MMM"
If you have an Azure back-end, with a column that is a DateTime or DateTimeOffset, the example code is rather sparse about how you send a timestamp value as part of an insert.
You can pass along an NSDate in the dictionary of values to insert, and the library will translate it for you and insert is as a UTC/GMT timezone value. However, my client specifically wanted this value to be in the timezone of the device which generated the data, which means I need to insert the value as a string, since NSDate has no inherent knowledge of timezones.
So...any suggestions on how to write the NSDate-to-string method?
How about this:
I changed my Azure Sql column datatype to datetimeoffset (which internally stores values as UTC as well as the timezone offset).
On client side I then used a date format with five Z's:
let date = NSDate() // local date time: Jun 27, 2014, 9:32 AM
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" // Note 5 Z's...
let strNow = formatter.stringFromDate(date) // "2014-06-27T09:32:49+02:00"
I was then able to insert the date string "2014-06-27T09:32:49+02:00" to my Azure table in that format.
Querying my Azure table for that same inserted date I received back:
2014/06/27 09:32:49 +02:00
There are two "gotchas":
The format is very specific for Azure to recognize and parse it correctly.
The required format is non-standard: the 'Z' specifier produces a timezone offset such as -0700 or +0800 but Azure will reject it if there isn't a colon between the hours and minutes, ie, -07:00 or +08:00. In the ARC solution below the colon is inserted after the string is generated.
(weird - format is a little off?)
+(NSString*)azureDateTimeString:(NSDate *)date
{
static NSDateFormatter *df = nil;
if(df == nil)
{
df = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] ;
[df setLocale:enUSPOSIXLocale];
df.timeZone = [NSTimeZone localTimeZone];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
}
NSString *dateString = [df stringFromDate:date];
// insert a colon in the third position from the right of the string...
NSString *newString = [NSString stringWithFormat:#"%#:%#", [dateString substringToIndex:[dateString length]-2], [dateString substringFromIndex:[dateString length]-2]];
return newString;
}
In my case I found I had to append "Z" as a literal suffix rather than a timezone type indicator as part of the format string:
(My Azure column is DateTime and I'm using Xcode Swift)
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let strNow = formatter.stringFromDate(date)
= "2014-06-27T00:40:52Z"
Note that I not only included 'T' as a literal but also 'Z'.
I could then successfully insert that date string into my Azure table and then, as a test, I could read it back and reverse it like this:
var creationDate = "2014-06-27T00:40:52Z"; //read back from azure table
let dtCreation = formatter.dateFromString(creationDate)
= Jun 27, 2014, 12:40 AM"
I need to show a date in concrete time zone including DST (European time). App will be used in Lithuania, so time zone is +3 at summer and +2 at other time. The thing is, I have just a list of dates and I don't know how to show +3 for summer dates and +2 for other dates. Currently, I have time zones:
// Eastern European Summer Time UTC + 3 hours
NSTimeZone *timeZoneWithDst = [NSTimeZone timeZoneWithAbbreviation:#"EEST"];
//Eastern European Time UTC + 2 hours
NSTimeZone *timeZoneWithoutDst = [NSTimeZone timeZoneWithAbbreviation:#"EET"];
But how to loop through my list of dates and calculate should I add +3 or +2 to date?
UPDATE Finally I got it working by applying Martin R. suggestion to use time zone by name, not by abbreviation. In this way, date with this time zone handles DST automatically. Here's my code for converting dates:
NSTimeZone *TimeZone = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
NSInteger seconds = [myTimeZone secondsFromGMTForDate:someDate];
NSDate *result = [NSDate dateWithTimeInterval:seconds sinceDate:someDate];
To convert an NSDate to a string representation, use NSDateFormatter. By default, it uses the local time zone. To display the date according to a concrete time zone, you can set
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"Europe/Vilnius"];
[dateFormatter setTimeZone:tz];
(According to http://www.timezoneconverter.com/cgi-bin/findzone, the time zone for Lithuania is "Europe/Vilnius".)
This is a very similar alternative that worked for me, in Swift:
var currentDate: NSDate {
let currentLocalTime = NSDate()
let localTimeZone = NSTimeZone.systemTimeZone()
let secondsFromGTM = NSTimeInterval.init(localTimeZone.secondsFromGMT)
let resultDate = NSDate(timeInterval: secondsFromGTM, sinceDate: currentLocalTime)
return resultDate
}