Date with Time only get's NULL - grails

I am using the Grails plugin, http://grails.org/plugin/jquery-date-time-picker
The date and datetime plugin works perfectly well. However, just time has an issue.
I tried to use time only for the datetimepicker. My Config.grovy is as below.
jqueryDateTimePicker {
format {
java {
datetime = "dd/MM/yyyy HH:mm"
date = "dd/MM/yyyy"
}
picker {
date = "'dd/mm/yy'"
time = "'hh:mm tt'"
}
}
}
My GSP code is something like.
<jqueryPicker:time name="openFrom" id="openFrom" value="${addressInstance?.openFrom}" pickerOptions="[dateFormat: '', timeOnly: true, hourGrid: '4', minuteGrid: '15', timeFormat: 'hh:mm tt']"/>
My Controller is
def addressInstance = new Address(params)
Here in the controller, i can see the params having the time as "7:00 am" but it never gets set in the addressInstance, i believe cause the date is missing.

The default Date binding takes the general date format as yyyy-MM-dd hh:mm:ss.S.
In order to to bind openForm date to addressInstance, you can explicitly set it as:
//where params.openForm is String like "7:00 AM"
addressInstance.openForm = Date.parse("h:mm a", params.openFrom)
Date.parse() parses the string as current format to return a Date object. In the aboved case(where you are only concerned about the time), you would end up with the epoch (Jan 1, 1970) with time as 7 AM.
def date = Date.parse("h:mm a", "7:00 AM")
//prints Thu Jan 01 07:00:00 EST 1970
//To get the time string from the date stored in db
date.format("h:mm a") //Prints 7:00 AM
You can also see if you can register a Custom property Editor as shown here to auto bind the date with customized format. You would not like to follow this if you do not want to apply the format to all date strings. In that case, I think the former approach will be useful and easy.

Related

SwiftDate string parsing returned 1 day less result

I'm trying to parse a date string into a Date class. I'm using SwiftDate for this. But when I tried to parse it, it returned 1 day less than the value in the string. Here are some examples:
let birthString = "1996-10-08"
self.birthday = Date(birthString, format: "yyyy-MM-dd", region: Region.current)
//Result: self.birthday = 1996-10-07 17:00:00 UTC
let expiredString = "2019-09-30"
self.membershipExpiredDate = Date(expiredString, format: "yyyy-MM-dd", region: Region.current)
//Result: self.membershipExpiredDate = 2019-09-29 17:00:00 UTC
How do I fix this error? Thanks.
SwiftDate uses regions, and you are setting the region to Region.current. The date is correct and you can check that using the .timeIntervalSince1970 property. What happens is that when you print the date (which is actually just a TimeInterval, aka Double, a specific point in time, independent of any calendar or time zone), it's printing its description property, with the default time zone UTC (You can see it in your output 17:00:00 UTC).
To print a date using your current locale, use this instance method:
print(date.description(with: Locale.current))

String did not Convert Properly to Date in ios Swift

I want to convert string to date everything is fine but still, it gives me an incorrect date according to string.
Code:
import UIKit
public class Utils {
public class func converServerTimeStampToDate (_ timeStamp: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy, hh:mm:ss a"
return dateFormatter.date(from: timeStamp)!
}
}
print(Utils.converServerTimeStampToDate("12/06/2017, 06:48:03 am"
))
-----OutPut-----
2017-12-06 14:48:03 +0000
To solve this problem set timezone
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
By default it is taking current time zone that's why your output is different from input.
It is correct result. When you convert any string into Date it will convert it in UTC timezone. And when you convert that date into string again it will be in your current timezone. So, convert that date into string and you real date you will get. So, your Date object always be in UTC timezone and your string will be always in your local timezone. And when you displays your date in label or any other control then definitely you will display in string format and it will be in your local timezone. So, there is nothing wrong in it. You just required to understand the concept!

Date formatter subtracting year by one and changing day when converting from string to date [duplicate]

This question already has an answer here:
Swift date formatting
(1 answer)
Closed 5 years ago.
I'm using dateformatter to convert a date between string and date types for inputing a date via a UIDatePicker. When the view loads if a saved string of the date exists it's put into the date text field (and entered correctly). When dateformatter converts the string into a date it returns the wrong date. (Always December 20ish of the previous year.)
let df = DateFormatter()
df.dateFormat = "MM/dd/YYYY"
func setDatePickerDate() {
if dobText.text != "" {
print("\n\nSaved date as string: \(dobText.text!)")
let testDate = df.date(from:dobText.text!)
print("Saved date converted to date: \(testDate!)")
print("Date converted back to string: \(df.string(from: testDate!))")
dobPicker?.date = df.date(from: dobText.text!)!
}
}
Which returns:
Saved date as string: 06/07/1977
Saved date converted to date: 1976-12-19 06:00:00 +0000
Date converted back to string: 12/19/1976
and if I keep running the setDatePicker function it keeps subtracting a year and changing the day.
Saved date as string: 12/19/1976
Saved date converted to date: 1975-12-21 06:00:00 +0000
Date converted back to string: 12/21/1975
Saved date as string: 12/21/1975
Saved date converted to date: 1974-12-22 06:00:00 +0000
Date converted back to string: 12/22/1974
Etc.
edit: Just noticed it cycles the days between 19-25 adding 1 to the date until it hits 25 then returning back to 19.
Change your format string to: "MM/dd/yyyy" and it should work as written.
Capital "Y" in Unicode Technical Standard #35 Locale Data Markup Language refers to "week of year" based calendars in which the year transition occurs on a week boundary. For most applications you want lower case "y", which refers to standard numeric calendar year.

Curious behavior while formatting dates

I'm getting a string representation of a date from a json that looks like the following:
let dateString = "2016-12-31T00:10:00+01:00"
In order to model it as a Date object I'm using a date formatter like so:
let dateForm = DateFormatter()
dateForm.locale = Locale(identifier: "fr_FR")
dateForm.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateForm.timeZone = TimeZone.current
When I turn it into a Date, my Playground output is correct:
let date = dateForm.date(from: dateString)
=> Output: "Dec 31, 2016, 12:10 AM"
But if I try to print this exact same object (date) I get the following output:
print(date!)
=> Output: "2016-12-30 23:10:00 +0000\n"
My question is: How can I be sure that I'm dealing with the correct date (by correct I mean with my local time zone (GMT+01)) ?
When you print the date output 2016-12-30 23:10:00 +0000 and your GMT is +00:00
but when you get the string from date it will return string as per your given format And your Locale (fr_FR) output 2016-12-31T00:10:00+01:00 and your GMT is +01:00
if you want to date from string then
date output = your string date - (your GMT)
In your case
2016-12-30 23:10:00 = 2016-12-31 00:10:00 - (+01:00)

Date format Convertion issue in swift

i could not convert string date into NSDate object.
Please check below code
let stringDate = "06:30 AM"
let formatter = NSDateFormatter()
formatter.dateFormat="hh:mm a"
let local = NSLocale(localeIdentifier: "en_US")
formatter.locale=local
let date = formatter.dateFromString(stringDate)
The output is as expected, and depending on what you're trying to achieve, you haven't really done anything wrong.
Your stringDate instance contains only information about a time of the day, not a date (the prior is also the only format your NSDateFormatter formatter is "interested" in). Hence, the following snippet produces the expected 06:30 AM output:
let stringDate = "06:30 AM"
let formatter = NSDateFormatter()
formatter.dateFormat="hh:mm a"
let local = NSLocale(localeIdentifier: "en_US")
formatter.locale=local
if let date = formatter.dateFromString(stringDate) {
print(formatter.stringFromDate(date)) // 06:30 AM
}
NSDate instances are defined, however, as single point in time (date and hour of the day), with reference to an absolute reference date:
NSDate objects encapsulate a single point in time, independent of
any particular calendrical system or time zone. Date objects are
immutable, representing an invariant time interval relative to an
absolute reference date (00:00:00 UTC on 1 January 2001).
From the language reference for NSDate.
Hence, in addition to a time of day, NSDate instances include also a date (even if this is not, in your case, used or displayed). When you assign a value to date above, the Swift playground displays the time of day of the correct date; the latter offset by 06:30 from the absolute reference date, 2000-01-01 00:00:00. If we modify the example above to print all details in the final print statement, we see this more clearly:
// ...
if let date = formatter.dateFromString(stringDate) {
formatter.dateStyle = .FullStyle
formatter.timeStyle = .FullStyle
print(formatter.stringFromDate(date))
/* Saturday, January 1, 2000 at 6:30:00 AM Central European Standard Time */
}
(Addition with regard to your comments below)
Note the difference of printing the date object itself (e.g. print(date) above) and printing a ** formatted string representation** of the date using your formatter (e.g. print(formatter.stringFromDate(date))). The prior just prints the .description property of your date, which is an default-formatted string representation of the contents of object itself rather than a controlled formatted output of the date:
Declaration
var description: String { get }
Description
A string representation of the date object. (read-only)
The representation is useful for debugging only.
There are a number of options to acquire a formatted string for a date
including: date formatters (see NSDateFormatter and Data Formatting
Guide), and the NSDate methods descriptionWithLocale:,
dateWithCalendarFormat:timeZone:, and
descriptionWithCalendarFormat:timeZone:locale:
Refer to my code blocks above to see how you can print the formatted date using your NSFormatter.

Resources