Is there a working jQuery plugin (or a javascript 'library') for formatting datetimes? I found some, but they were:
not working with hours and minutes (the one from datapicker)
not fully functional - can't give you names of months, leading zeroes, etc.
are just a piece of code written in some blog.
Of course I can implement it, but it'd be better to reuse one. I seek functionality similar to Java's SimpleDateFormat
I've written a JavaScript implementation of the format() method of Java's SimpleDateFormat: http://www.timdown.co.uk/code/simpledateformat.php
The code is a few years old and I'd do it a bit differently now, but it's well tested and works.
Did you try date.js ?
It has a pattern recognition to format dates that is easy to use and has plenty of localisation files available.
ie: Date.today().toString("d-MMM-yyyy HH:mm")
I use http://blog.stevenlevithan.com/archives/date-time-format
Not sure whether it fits all your requirements, but this looks good one:
jquery-dateFormat
Related
Up until now, I've been using Moment.js durations feature to handle all my "length of time" needs. As Moment.js is no longer being supported, I would like to move away from it. However, I've used it for so long and now find it difficult to transition. What are other methods I can use to handle durations?
You can try dayjs library or native JS Date objects.
I have millisecond and i want to convert it in format Feb-01-2014 09:12:12. I have used following code
var today = new Date(1419359400000);
var p=today.toLocaleFormat('%b-%d-%Y %H:%M:%S');
But it is not working in chrome
Thanks
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat reads:
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Use some date library if you find yourself needing to formats a lot or use the native functions today.getYear(), today.getMonth() etc.
Keep in mind that toLocaleFormat uses the operating system locale which might not actually be what you want. Your dates might appear in a different language than the rest of your application.
i want do number formatting in Javascript.. and i use the following method
num.toLocaleString() which will work for Firefox, IE but doesnt work for Google Chrome..
Wat i need to add for it work in chrome browser.
The toLocaleString() method is by definition implementation-dependent: it uses the implementation locale, such as browser locale. So if I were looking at your page that uses the method, I would see numbers formatted according to Finnish or English locale, depending on which browser I’m using.
What you want is localization by the locale of the page, and for this you need something else. In simple cases you might code it yourself, but number formatting is in general complicated, making it reasonable to use a library, such as Globalize. Check out the compact source of a simple demo. In Globalize, you use standard language codes when specifying the locale.
Internationalization is always challenging and unfortunately there doesn't seem to be a consistent/pervasive solution to it. Your best bet is to use a 3rd party library to take care of things for you.
We rely heavily on googles closure library, which has some pretty powerful i18n (internationalization) tools. Take a look at http://www.daveoncode.com/2009/11/26/goog-i18n-numberformat-formatting-number-locale-string/ for an example of how to use it. In the end, it becomes as easy as:
// define italian number format symbols
goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_it_IT;
// create new decimal formatter (PERCENT, CURRENCY, SCIENTIFIC are options)
formatter = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
// view formatted and localized string
alert(formatter.format(15650.579));
If you are new to closure, don't worry. It's not hard to get set up and has a multitude of excellent helper classes that you may find useful.
http://code.google.com/closure/library/docs/gettingstarted.html
The JavaScript internationalization support is quite poor (as you have discovered).
You might take a look at https://github.com/jquery/globalize
It handles number formatting, and also dates, times, currencies.
A bit of voodoo can implement your own number formatting. You could build this into String.prototype, but I didnt want that, since its localized.
function reverse(str) {
return str.split('').reverse().join('');
}
function num2str(num) {
var str = num+"";
// european
// return reverse(reverse(str.replace('.',',')).replace(/\d{3}/g,'$&.').replace(/\.$/,''));
// american
return reverse(reverse(str).replace(/\d{3}/g,'$&,').replace(/\,$/,''));
}
and then its
> console.log(25000.45)
> 25,000.45
I'm looking for a JavaScript library which can parse and format date values and numbers. It should not be too big or complicated just 4 methods would suffice for me: parseDate, formatDate, parseNumber, formatNumber
//dt is a JavaScript date object, str a string containing a date, formatoptions specifies the format
var dt = parseDate(str, formatoptions);
var str = formatDate(dt, formatoptions);
//same applies for parseNumber and formatNumber just replace dt with nr which represents a JavaScript number
I've already asked Mr. Google but there are sooo many libraries and articles out there that it is hard to find something good. Many libs can format a number or date but can't parse them. Or they are not stable (reasonably bug-free).
Any recommendations?
update:
Sadly serverside processing is not really on option. I know support for date and time manipulation is much better in PHP or ASP
I included some sample code what I would expect from the parse and format functions
update 2:
I found quite a good library for formatting numbers which is called jquery-numberformatter
I have been using date.js from http://www.datejs.com. The parser in the home page will give you an idea of its capabilities.
These days I would look at MomentJs. Theres plenty of documentation and tests.
I'm registering a javascript in aspx.net
Dim script = "<script language = javascript>" & _
"window.setTimeout('ShowTime(true, [?????])', 1000);</script>"
ClientScript.RegisterStartupScript(Me.GetType, "iniciar", script)
I must write a dateTime parameter in string format, but I'm not achieving the goal. I've tryed various time formats (Eg. 2011/02/10 17:05:00), without success. Ps: I know... when I try with only the date, its ok. But I need the time too. Thanks.
Javascript lacks a comprehensive string-based format mechanism. Aside from manually piecing the date together using the Date object's methods like getMonth and getFullYear, one would have to create a script to do such formatting, or use one of the myriad existing functions on the internet. Here is an excellent example: http://blog.stevenlevithan.com/archives/date-time-format
Date.js is a great library for all date-related function in Javascript.