let stringWithDate = "2015-07-16T6:08:32.000Z"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateComponents = NSDateComponents()
dateComponents.year = 2015
dateComponents.month = 7
dateComponents.day = 16
dateComponents.hour = 6
dateComponents.minute = 8
dateComponents.second = 32
let dateFromDateFormatter = dateFormatter.dateFromString(stringWithDate) //"Jul 16, 2015, 8:08 AM"
let dateFromDateComponents = NSCalendar.currentCalendar().dateFromComponents(dateComponents)! //"Jul 16, 2015, 6:08 AM"
dateFromDateFormatter == dateFromDateComponents //false
Why these dates are not the same?
After some digging, I have found a solution:
NSDateFormatter includes information about NSTimeZone (Z at the end of Sting) while NSDateComponents doesn't. So we need to simple inform dateComponents about NSTimeZone:
dateComponents.timeZone = NSTimeZone(abbreviation: "UTC")
The output dates are now the same.
Adding the same information to dateFormatter:
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
doesn't change anything, because there is information about NSTimeZone yet.
Related
I am getting Date as '2/12/2020 4:30:29 PM'. But i need trimmed year and seconds date time format like
'12/02/20 04:30 PM'
what is the equivalent function in MEL for getting above date time?
Thanks
I'm afraid there isn't an equivalent function in MEL to do this thing. BUT the good news is that you can create it! (yes, MEL needs more time functions to work...).
I've create this global function that you can use it (I assume 2 is day and 12 is month, if not you can change the order):
separator = "/";
space = " ";
hourSeparator = ":";
$global:stringDatetimeToArray = function(datetime)
{
array['year'] = substring(datetime, 6, 10);
array['month'] = substring(datetime, 3, 5);
array['day'] = substring(datetime, 0, 2);
array['hour'] = substring(datetime, 11, 13);
array['minute'] = substring(datetime, 14, 16);
array['seconds'] = substring(datetime, 17, 19);
array['meridian'] = substring(datetime, 20, 22);
return array;
};
concat(array['month'], separator, array['day'], separator, array['year'], space, array['hour'], hourSeparator, array[minute], space, array['meridian']);
My recommendation for you is to generate a method that converts a timestamp into an array of these values, and then you can work with these kind of issues easier than now. You can see an example on this github script
import re
date_str = '2/12/2020 4:30:29 PM'
sub_str = re.search(':\d+(.*?)\s',date_str).group(1)
date_str = date_str.replace(sub_str,'')
print(date_str)
Output: 2/12/2020 4:30 PM
Starting Swift 4, I have one unit test that starts to fail. It seems that the timezone data I get is quite incorrect.
The following working code:
let f1 = DateFormatter()
f1.dateFormat = "yyyy-MM-dd"
let d = f1.date(from: "1999-01-01")
let f2 = DateFormatter()
f2.dateFormat = "Z"
f2.string(from: d)
I'm in America/Los_Angeles, so I get -0800
If I swap 1999 to 1500 with this code:
let f1 = DateFormatter()
f1.dateFormat = "yyyy-MM-dd"
let d = f1.date(from: "1500-01-01")
let f2 = DateFormatter()
f2.dateFormat = "Z"
f2.string(from: d)
I get -075258
Is this even a correct/allowed value? How can I get something valid for usage in a ISO 8601 ?
I have a string want to convert to date, but below code,
that str
print out show Optional(2016-04-25 17:00:16 +0000)
I want to know how to show exact what it is like Optional(2016-04-26T03:00:16.047)
var str = "2016-04-26T03:00:16.047"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString(str)
print(date) //Optional(2016-04-25 17:00:16 +0000)
I have this String 2015-02-17T08:53:22.9170000+00:00 and I want to converted to NSdate.
The prefered format to print is hh:mm:ss-dd-mm-yyyy.
Try this one
let string = "2015-02-17T08:53:22.9170000+00:00"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz"
if let date = formatter.dateFromString(string) {
//Now you can format what ever you want, like
formatter.dateFormat = "hh:mm:ss-dd-mm-yyyy"
print(formatter.stringFromDate(date))
}
i am working in .Net Charts. i want to show date in X-Axis. For ex : if i select Last 52 Weeks, then i should show the chart for last 52 weeks, whereas those 52 weeks start date should be in x-axis. I am not having any idea, how to do this..I had tried with the code..
DateTime Frm = sessionManager.ChartViewPeriodFrom;
DateTime To = sessionManager.ChartViewPeriodTo;
double min = Frm.ToOADate();
double max = To.ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Here i am getting the Frm as "9/17/2011 12:00:00 AM" But, in the chart the minimum date starts from "9/21/2011 12:00:00 AM". How to fix this...
I had tried like this also..[ Edited Part ]
Chart1.Series["Series1"].XValueType = ChartValueType.Date;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = (new DateTime(2011, 09, 17, 12, 00, 00)).ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = (new DateTime(2012, 09, 08, 12, 00, 00)).ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
After a long search...i had fixed my above issue...but still i dont know how it works...
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart1.Series["Series1"].XValueType = ChartValueType.Date;
DayOfWeek ds = DayOfWeek.Wednesday;
double dblIntervalOffset = Convert.ToDouble(ds);
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = dblIntervalOffset;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
Indexed attribute must be set for series data.