Since my date is well formatted now all there is left for me to do is to print milliseconds too.
I already tried setDateFormat yyyy/mm/dd hh:mm:ss:ms
That didn't work. Any advice?
Try to use 'SS' specifier (with number of S's equal to number of digits you want to get - 3 in your case), e.g.
[formatter setDateFormat:#"yyyy/MM/dd hh:mm:ss:SSS"];
Try this format:
"yyyy/MM/dd HH:mm:ss.SSS"
Related
I am trying to covert start_time which is in yulu format to pst.
Start_time sample: 2020-02-04T04:36:42:211Z
from_unixtime(unix_timestamp(sub string(start_time,1,17),'yyyy-MM-ddThh:mm:ss.SSSZ),'yyyy-MM-dd hh:mm:ss)
But I am getting output as NULL.
Please help.
Escape T, Z in the string. Note the use of double-quotes for the pattern and T and Z are escaped with a single-quote.
select from_unixtime(unix_timestamp('2020-02-04T04:36:42:211Z',"yyyy-MM-dd'T'HH:mm:ss:SSS'Z'")
,'yyyy-MM-dd HH:mm:ss')
Also, you don't need a substring as you are matching the pattern for the full string.
It is better to use FROM_UTC_TIMESTAMP because UNIX_TIMESTAMP returns seconds, you will lose the millisecond component of your timestamp.
FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(2020-02-04T04:36:42:211Z, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"), 'PST')
Sometime it might possible that because of "T" and "Z" our result get distorted.In that case we can use:
from_utc_timestamp(CONCAT(substring('2020-02-04T04:36:42:211Z',1,10)," ",substring('2020-02-04T04:36:42:211Z',12,12)),'PST')
How would I convert this string recentUpdate to an NSdate? My recentUpdate outputs this 10/05/2014 06:43:PM and when a converted it to NSDate, it always results null.
Can someone please tell me what I am doing wrong? Any tips or suggestions are appreciated. My question is a little more specific compare to the other questions on stackoverflow so this isn't a duplicate question.
NSString *recentUpdate = sharedData.mUser.mRecentUpdateTime;
NSDate *date = [NSDate dateFromString:recentUpdate usingFormat:#"MM-dd-yyyy HH:mm:ss"];
Your format does not match the string you gave:
MM-dd-yyyy HH:mm:ss
10/05/2014 06:43:PM
^ ^ ^^
Here and here
If you are converting any string to NSDate, make sure your string format should be same as the format you are passing in date-formatter.
Example -
for string like 10/05/2014 06:43:PM, your date format should be - MM/dd/yyyy HH:mm:a
for string like 10-05-2014 06:43 PM, your date format should be - MM-dd-yyyy HH:mm a
otherwise result will be null .
I'm trying to make this line work:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "yyyy-mm-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
I'm getting date/time strings from spreadsheets in the above format, and I can't control that. There's a ton of help online, including this site, about converting strings to dates, and I've tried them all, but I keep getting this error :
"System.FormatException: String was not recognized as a valid DateTime."
I"m just about ready to write my own custom parser, but that doesn't seem very elegant. Is there some built-in way to convert a string like mine into the date/time format I need?
Thanks for any help.
Your format string is wrong. You're entering a date in d/M/yyyy hh:mm:ss tt format, but telling it to expect a date in yyyy-mm-dd hh:mm:ss format. The two do not match exactly, so DateTime.ParseExact is quite rightly throwing an exception at you.
Try:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "d/M/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
This tells it to expect the following characters:
Integer from 1 through 31 (depending on the length of the month)
/ character
Integer from 1 through 12
/ character
4 digit year
Space
Integer from 1 through 12
: character
Integer from 00 through 59
: character
Integer from 00 through 59
Space
Two character meridian specifier ("AM" or "PM")
For more info on the datetime format strings, check out this MSDN page
I think you need to change the string format to match what you are passing in. You seem to be passing in something more like this:
"d/M/yyyy hh:mm:ss"
Give that a try and see how it works. Note that you need to use MM for months--'mm' is used for minutes.
In my application i get from a server some dates.
Some of the dates are in this format : "MMM dd, yyyy hh:mm:ss a" and some others in this : "yyyy-MM-dd HH:mm:ss Z" . I get the dates as strings from the server in one of the above formats. However i want all my dates to be in the first format. So how can i compare if the date is on the second format so i can change it? I know how to change formats , i just dont know how to compare a date with a specific format.
Thanks!
If you know the format that any given returned string is in then you could explicity set the format for each iteration of getting a date and then convert it to whatever format you need. Something like this example;
NSString *myString1 = #"01-30-2012";
NSString *myString2 = #"30-01-2012";
// Now convert the string to a date object
NSDateFormatter *myDateFormat = [[NSDateFormatter alloc] init];
[myDateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date1 = [myDateFormat dateFromString:myString1];
[myDateFormat setDateFormat:#"MM-dd-yyyy"];
NSDate *date2 = [myDateFormat dateFromString:myString2];
[myDateFormat release];
If those are the only formats, just check something simple like if the fifth character is a dash then it's second, otherwise first.
I need to retrieve the current date with this format:
yyyy-MM-dd-hh-mm-ss-XXX
But which are the letters to use for the milliseconds?
As is documented by Unicode, S means fractional second:
Fractional Second - rounds to the count of letters. (example is for 12.34567)
Use as many S as needed for the given precision.
The letters for miliseconds are SS, so you should do:
yyyy-MM-dd-hh-mm-ss-SSS