How can I change formatting of date in javascript? - javascript

How can I change a date from the form 01/01/02 to January 1st, 2002?
Can I do this in javascript? If not, jQuery?

While you can certainly write your own date formatting routine, your best choice is using a library like moment.js. That is especially true if you'd be doing some manipulations to those dates besides just displaying them. Dates are hard to do and there are many things to consider (i.e. timezones)...

Using momentjs:
var x = moment('01/01/02', 'mm/dd/YYYY').format('MMM Do, YYYY');
console.log(x);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

dateObj.toLocaleDateString() in javascript might be helpful

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.

Comparing 2 Times with DateJS

I've seen a few different questions on here regarding finding the difference between using two different dates.
My question is similar to Parse ONLY a time string with DateJS.
I basically have 2 time inputs:
<input id="start_time" type="text">
<input id="end_time" type="text">
The format of these will always be: 07:15 AM or 08:30 AM
Essentially, what I am trying to do is ensure the start_time is not greater than the end_time.
I have tried using DateJS to parse the date, but it returns null:
Date.parseExact("03:15 PM", "HH:mm"); <--- returns null
How should I go about comparing the two input fields (using DateJS or something else) to ensure the start_time is not greater than the end_time?
Any help would be great.
You need to add the AM/PM field to the format you're using to parse with. Try using
HH:mm tt
Instead of
HH:mm
Not exactly answering the question, but DateJS looks a bit outdated. I would suggest you take a look at Moment.js, where you can do this:
moment("03:15 PM", "hh:mm A").isAfter(moment("03:05 PM", "hh:mm A"));
// false
Why not using toString?
var g = new Date("2012-01-11 03:15 PM");
console.log(g.toString('HH:mm'));
just add a ficticious date in order to obtain a valid date string format.
Since I guess you just need the Time not the date
It works fine with me.
You can use Date.parseExact with the format revision presented by #matthewtole, or you should be able to just use 'Date.parse' as well.
Date.parseExact will provide better performance, but if you're just parsing two values, the performance improvement of Date.parseExact is probably not going to make much difference.
You can also use the .isBefore() function to check if one Date occurs before the other.
Example
var d1 = Date.parse("07:15 AM");
var d2 = Date.parse("08:30 AM");
d1.isBefore(d2); // true
Hope this helps

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

How to change the date format in JavaScript?

I am using one jquery date picker,
with using picker i am getting date in like this format
Friday, May 21, 2010
Now i want to add one day in this date so i think, i can only do if i change the date in exact format like
21/5/2010
I want to only convert that bcz i want to add one day to the particular date.
So what do u suggest me? How can I do that?
Can i do without converting it ?
thanks in advance....
Take a look at http://docs.jquery.com/UI/Datepicker#option-dateFormat
datejs may be useful to you.
In addition to the formatting options given by others, you should add using date objects rather than to the string representation of the date object.
I.E.
// add 5 days to today
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);
I think you need to detail what jQuery plugin do you use.
Is it this one? http://jqueryui.com/demos/datepicker/
If so, then when you cann getDate method, you'll get Date object representing a date. You can easily manipulate it.
The date format has nothing to do with how dates are stored; it only affects the way dates are displayed. JavaScript has a native Date object and jQuery UI's Datepicker allows to access such object:
http://jqueryui.com/demos/datepicker/#method-getDate
Once you have a Date object, you can alter it to suit your needs:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
Finally, you can feed it back into Datepicker:
http://jqueryui.com/demos/datepicker/#method-setDate

Categories

Resources