JSON date, display original date in the server's timezone - javascript

For example, I have a string date like this (I'm getting this from the server in json, from rails app)
s = "2013-09-01T00:00:00.000+08:00"
I would like to display it like so
01.09.2013
So I'm using moment.js library for this
moment(s).zone("+08:00").format("DD.MM.YYYY")
>> "01.09.2013"
But I don't know if needed timezone is +08:00. If I skip .zone() call, result would be wrong because my browser is in differnt timezone
moment(s).format("DD.MM.YYYY")
>"31.08.2013"
Even though in my original string I had +08:00 at the end.
So, my question is how can I extract time zone from json date string using pure javascript or moment.js library?
The simplest way I can think of is extracting the last 6 characters manually,
s.slice(s.length - 6, s.length)
> "+08:00"
But maybe there is a better approach for this task?

Just use the parseZone function, like so:
moment.parseZone(s)
Documentation is here.
Alternatively, you can use the older approach, which does the same thing:
moment(s).zone(s)

Related

Converting JavaScript date to JSON/.net date format

I have a regular date i.e.:
date= 03-12-2014
I need to convert it to JSON or .Net date format. Like this:
"\/Date(1283219926108)\/"
I can see a lot of posts that go from JSON date to regular date but not backward. Please let me know how to do it. I am hoping for some easy JavaScript way to do it.
Do you know/Have you tried (new Date).getTime()
That's the most easiest "cross-browser" solution I know of ...
In your situation something like:
(new Date(date)).getTime()
It's a little bit vague to see what the difference is between .net- and javascript code.
Can you point it out more clearly?
Take a look at this little jQuery file. https://gist.github.com/gigi81/1868478
It does date parsing for .NET.

Moment.js 2 different date strings when i parse using moment gives same value

I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.

How can I make go Time compatible with js Date

When I define time like this in js
{expiry:new Date()}
and create a struct in go endpoints like this
{Expiry time.Time `json:"expiry"`}
I get a parse error from go
"parsing time \"\"2006-01-02T15:04:05Z07:00\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \"07:00\"\" as \"\"\""
Any suggestions?
The documentation for time.UnmarshalJSON states:
UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.
There is a problem that all browsers doesn't necessarily encode DateTime objects into RFC3339 format. However, your error message doesn't seem to imply that. You seem to try to encode the following JSON string:
"2006-01-02T15:04:05Z07:00"
That is not a timestamp, but rather the time package's reference layout. See this Playground example that shows how Go expects a timestamp to be like: http://play.golang.org/p/4NQ1pRidPt
However, there is still that problem with browser inconsistency. To avoid this you can use a function or library, as #elithrar suggested:
var a = {expiry: moment(new Date()).format("YYYY-MM-DDTHH:mm:ssZ")};
console.log(a);
Output:
{"expiry": "2014-01-08T08:54:44+01:00"}
JSFiddle

JSON datetime between bash script and JavaScript

In the same spirit as discussed here, is there a recommended way to generate / parse dates from within a bash script so that it can be interfaced to Javascript Date?
To be precise, I get this strings when doing json encoding of a Javascript Date object:
2011-10-31T10:23:47.278Z
I could put together a bash hack to generate / parse that date format, but I would prefer to avoid reinventing the wheel. Does somebody have a working solution?
I am more interested in the "generating" side: I want to generate current dates from a bash script and save them in a json document (couchdb) so that they can be automatically ordered by the view engine.
The closest I am coming is this:
date -u +"%FT%T.000Z"
Which gives this output:
2011-11-03T06:43:08.000Z
I do not like that I have to put the T, the Z and the milliseconds to 0 manually (I can use %N for nanoseconds, and truncate with sed or whatever, but seems like overkill just to get millisecond precission), and I was hoping that there would be a built-in format token for date which would produce that UTC date. I assumed - wrongly it seems - that the format is common enough that it can be specified with just one format token.
JavaScript can convert many different values into dates. Not sure if that's what you mean, but for example. Your bash could generate this string: "2011/11/10 08:08:08"
When it gets to JavaScript land you can do this
var date = new Date("2011/11/10 08:08:08")
You can also do this:
var now = 1320287813362
var date = new Date(now)
More info on what Date accepts here:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Other interesting info here:
What's the best way to store datetimes (timestamps) in CouchDB?

javascript date manipulation

I have a string
2010-08-02 12:13:06.0
and need to get something like
Fri Aug 6 2010
out of it (the input does not map to the output for the values I gave, just examples)
I fear Im going to have to do some string manipulation to get what I want; the js Date object does not seem to have methods capable of parsing the input string.
Is this correct?
We are using jquery, but cant find anything in that library that would help...
Everything has been invented before us:
http://www.mattkruse.com/javascript/date/
You can use the date object for this. Just parse the first part of the date string to get the individual numbers and use setFullYear(), setMonth(), setDate(). You will have to subtract 1 from the month, but then use the toDateString() and it outputs it like your example. http://www.w3schools.com/jsref/jsref_obj_date.asp

Categories

Resources