in my json i've this date generated by JsonConvert.SerializeObject()
2020-05-11T15:27:50.3666678+02:00
I get this json by jquery ajax call and i need to set this value (client-side) to an html helper tag
to set this field client-side i do:
$("#scandate").val(details.ScanDate);
where scandate contains the date in the format above 2020-05-11T15:27:50.3666678+02:00
but i got this error:
The specified value "2020-05-11T14:41:59.5753508+02:00" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
trying to pass a (fixed) date like this 2020-05-11T15:27:50 the component shows the date!
how can i adjust it avoiding to do a brutal subtring in order remove the last part of the date?
In rails I have DateTime object as:
2014-08-21 18:14:12 UTC
I want to display it as:
08-21-2014
I can hack this as follows:
"2014-08-21 18:14:12 UTC".to_s.sub(/(\d*)(-)(\d*-\d*)/, '\3\2\1').first(10)
But I prefer the proper Rails/ Ruby way and not having to convert the Date object to a string. Thanks!
See DateTime#strftime:
> DateTime.now.strftime '%m-%d-%Y'
=> "08-22-2014"
And please, please, please don’t use that format, unless you have no choice. Hyphens should only be used for the Y-m-d format. Use slashes or dots if possible. (Even better, use Y-m-d!)
Just use strftime to get various formats of date & time.
> DateTime.now.strftime("%m-%d-%Y")
=> "08-23-2014"
Read more about strftime
I need to produce a date in Rails which looks like this:
/Date(1294268400000)/
I have tried various combinations of DateTime, to_i, to_json but never managed to get the /Date()/ thing.
Do I have to simply get my date in ms and then wrap the /Date(and )/ manually, or is there a built in method?
What about (ruby 1.9.x)?:
Time.now.strftime("/Date(%s%L)/")
=> "/Date(1335280866211)/"
You should try
new Date(posixMillisecondsHere)
first. MDN says that calling the Date function outside of the constructor context (i.e., without the new) will always return a string containing a formatted date rather than a Date object.
Strictly speaking, when you do that, you are writing JavaScript and not JSON. JSON cannot contain Date objects.
RFC 4627 says
2.1. Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
false null true
If you want to put a Date into what is strictly considered JSON and then get it back out, you must choose some way of using the JSON primitives (to wit, objects, arrays, numbers, strings, etc.) to encode a Date.
If you want to get a Date back out of JSON, whatever parses your JSON must understand the convention that you used to encode the Date.
Hope these are credible and/or official enough to help.
What about something like this:
in your config/en.yml file:
en:
time:
formats:
json: "/Date(%s%L)/"
and than in the view:
<%= l(Time.now, :format => :json) %>
Please note that you would need access to the helpers in the method that renders json. So it won't work if you are using ActiveRecord#to_json method for generating jsons.
Check out this question:
c# serialized JSON date to ruby
... simple answer seems to be to create a parse_date method.
It's the UNIX Epoch (seconds since 1970-01-01) right? What about using DateTime#strftime method?
# Taken from the Ruby documentation
seconds_since_1970 = your_date.strftime("%s")
UPDATE: OK, it's milliseconds, according to the documentation you can use your_date.strftime("%Q") to get the ms (but I've not tried yet).
I am trying to implement the DateRange extension into a MVC site.
The site is trying to use this date format: yyyy/mm/dd (ie 2011/02/14).
When I try a 'Create New' on a page the jquery-ui.js throws an unhandled error on the parseDate function. The parseDate functions shows 'm/d/yy' as the format and '2011/02/09' as the date value.
What is confusing me is how after changing all the m/d/yy formats to yy/mm/dd in the code that there is still the other format showing.
After constant tweaking the sample the only thing that appears to influence the date format is the 'Date.cshtml' file. Changing this files format string will influence how the initial(default) date-time values show. Changing the other C#/JS files seems to have no influence on how the datepicker displays the dateformat.
As a result of this I will be dropping this extension from my project...
I'm POSTing json data to a Grails controller which I then parse using JSON.parse.
It all works fine except for date fields. I don't believe there is an explicit syntax in json to represent a Date but I've tried a number of formats with no luck. Can anyone tell me what format I should use so that the grails JSON parser can create a Date object.
There isn't a specific format, but you can define your own. For example, these guys here are adding a '#' to the beginning and the end of the string.
According to Grails docs here, you can define:
grails.converters.json.date (String) - Configure how Date values
are serialized to JSON
"default" - String representation according to the JSON specification
"javascript" - new Date(...)
Update: It appears that there is no mapping to Java Date objects. If you know the fields that are dates, you can parse them into Dates