Converting JavaScript date to JSON/.net date format - javascript

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.

Related

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.

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

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)

Javascript - change SQL datetime format

I'm still new to programming, and am having trouble converting this date. I'm using ajax with a chat application, and pull the date from SQL Server database record but can't seem to convert it. The line of code getting the date is simply:
var timeStart = results.d[i].CreateDate;
the result I get is: /Date(1365692153250)/
I tried adding 'new Date( )' before, and/or '.format(MMMM ...)' after. This is probably an easy one, but I've looked all over. Please let me know if any additional info is needed. Thanks.
I think this question and answers will help you:
Convert a Unix timestamp to time in JavaScript
You just need to convert your timestamp.
Good luck.
You will want to construct a Date() and use the getters to build up the format.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
For passing in a format, you will want to include your own library, here is one solution for generating it:
http://blog.stevenlevithan.com/archives/date-time-format
Thanks #mattmanser for this thread, which answered my question... Converting .NET DateTime to JSON
Solution:
var timeStart = results.d[i].CreateDate.replace(/\/Date\((-?\d+)\)\//, '$1');
var d = new Date(parseInt(timeStart));
I hate to 'answer my own question' but want to provide the solution to anyone else who may need it.

Javascript equivalent to Ruby Time.parse

I allow users to enter datetime in whatever format they like as long as Ruby's Time.parse method can handle it. Now I need to compare these datetimes in javascript. Is there anything equivalent to Ruby's Time.parse method for javascript?
I need something that can parse for instance "October 13 2012 at 8:15am". I tried datejs but it couldn't handle the "at" word. I would really prefer something that only requires a single function call.
The JavaScript language is picky (and implementations vary) regarding what date format it accepts, so there's no hope without using a library.
Date.js is easily your best bet; however, as you point out, there are sure to be formats that make perfect sense to users but that the library couldn't anticipate.
To workaround, I suggest that you wrap the Date.js parser in a custom scrubbing function that you must maintain:
// using Date.js
function parseDate(str) {
var wordsToRemove = ['on', 'at'] // ...
, regex = new RegExp('\\b(' + wordsToRemove.join('|') + ')\\b', 'g');
return Date.parse(str.replace(regex, ''));
}
Datejs has a really impressive stuff to work with dates in javascript
Try http://momentjs.com/, best time library ever!
This is a bit late, but hopefully it will still help:
If you're only parsing US locale date strings, check out Sherlock.js.
The only limitation is that it doesn't support specifying years or past dates. It can parse 'October 13 2012 at 8:15am' but it will return a Date object for October 13, 2013 at 8:15am, since it always looks forward. You could write some RegEx, like /\b20\d\d\b/ to parse out the year from the string yourself though if needed.
Disclosure: I am the creator of Sherlock.js

get date format by locale code javascript

I was wondering whether there is a special function to be used in javascript to get a dateFormat by locale code.
String dateFmt = function_get_date_format_by_locale_code ('en');
would return MM/dd/yyyy.
Not an exact answer to the question, but you could try using date.js, a great little library for working with dates in JavaScript. It has some locale functionality/support I believe...

Categories

Resources