Identifying the date format in the client's machine - javascript

I have a requirement in my project to show a date in the client machine format. I am aware of the property in javascript window.navigator.userLanguage or window.navigator.language.. But it is only returning the language of the client machine like 'en-US', 'en-GB' etc.
If the user customizes the date format of the machine for eg:-dd-MMM-yyyy, is there any way to get that format in Javascript?
Thanks in advance
suhaib

You can use the .toLocaleDateString() method:
var yourDate = new Date();
alert(yourDate.toLocaleDateString());
This doesn't tell your code what the user's selected date format is, but it lets you display a date in whatever the user's format is.
On my PC the above alerts "Tuesday, 26 June 2012".
The .toLocaleTimeString() method does the equivalent thing for the time.
The .toLocaleString() method displays date and time.

Related

DateTime.Min JSON Serialisation Incorrect

I am trying to figure this strange behaviour on a client machine (IE10)
When we create a new javascript Object, the ajax response from the server is \/Date(-62135596800000)\/.
I format the JSON Date in the following way:
var date = new moment(parseInt(response.substr(6)));
On the Client machine this Date Object returns the following Date Format 0000-12-31.
When I try to validate the Date on the Server I get the response is an error saying that this is not a valid date.
My validation is Fluent Validation and the Rule is
RuleFor(x=>x.LastUpdateDate).NotEmpty();
Can anyone point me in the correct direction to help solve this issue?
Your timestamp value is interpreted as being an offset from a fixed UTC reference point. However, when you do something like
alert(theDate)
you'll see the default rendition of the Date instance as it would appear in the local time zone. In other words, a computer in Hong Kong will show the same UTC date differently than a computer in London.
You can use
alert(theDate.toUTCString())
to see a UTC version of the date.

How to convert a date which is in user timezone to JST or any other timezone using Date() in JavaScript?

I want to convert a date which i get from new Date() to JST timezone or any other timezone without using any JavaScript library. The method should be generalized so that in future if i want to switch to any other timezone, it should be able to do so.
Timezones are not that straight forward (some locations do not follow the rules), so you will never get that accurate without a lib which includes a DB of locations e.g., https://github.com/mde/timezone-js
However, I believe you're looking for getTimezoneOffset()

How do I convert a new Date() into the client's time?

I'm trying to get
date = new Date()
and then make it into a local time from the client, how would I go about doing that?
I would need the Date, in this format
Day[Mon,Tues, etc.], Day of the month[18], Month[Nov,etc.], Year, then the time in either a 24 hour format or a 12 hour format, seconds would be good.
I've tried using the getTimezoneOffset(), but I'm not sure how to use it.
Dates in JavaScript should always be in the format of the client computer.
If toLocaleDateString isn't working for you, I recommend Moment.js
Using toLocaleDateString:
console.log(new Date().toLocaleDateString());
Using Moment.js:
console.log(moment().format('llll'));
By a "local time from the client" I assume that your server returns some time from, let's say, GMT-0 , and your client is at GMT-3.
Knowing your server timezone, you could do some trick to do the math using jsTimezoneDetect (http://pellepim.bitbucket.org/jstz/).
And regarding the statement "Dates in JavaScript should always be in the format of the client computer.", that's assuming you are using Javascript on the client side, while in fact, you could be using it on the server (nodes).
BTW, when you say "I'm trying to get date = new Date()" I'm assuming you're doing that on the server with nodejs, otherwise, both jasonscript and Loper324 are right, you are already in the client so that Date will be in the timezone of the client. Or were you referring just to the formatting?

how to get local date/time format?

I want to get systems local date/time format and display date from database on this same format. e.g if system date format is like Month dd/yyyy and date stored in database is mm/dd/yyyy format then I need to get local date/time format and convert my stored date into local format.
HTML5 input type="date" this takes default systems date/time format and show date in same format. how can I do this with jquery or javascript.
You could use the JavaScript Date object:
var date = new Date(yearFromDb, monthFromDb, dayFromDb);
date.toLocaleDateString();
EDIT
It seems that the above method is not consistent between different browsers (Chrome, IE, Firefox). Therefore, I think your only option is to get the formatted date string from server side.
Here is a jsFiddle that shows a cross-browser way of getting the users locale, if you can use this to get a formatted date string in the current locale from the backend (if any).
var locale = window.navigator.userLanguage || window.navigator.language;

Date format on client browser

I am getting a date from the server as a javascript string(GMT) or unix timestamp(GMT).
I can convert it to a javascript date object with
var date = new Date(string) or var date = new Date(string)
This gives me the date variable with proper system time-zone's time.
Currently I am displaying this- date.toLocaleString()
Which gives me a nicely formatted date/time according to my system locale and time zone.
I am using this to automatically accomodate for DST if the client browser follows it.
Previously I was required to display this date only in EST, but when the US time started following EDT, I was told to display it in EST. I think this approach would simplify displaying the time/date acoording to user's system time setting.
Is there any disadvantage or possible bug accociated with this approach?
If yes, what would be the best way to display this date in the browser, so that it displays correctly according to the timezone(accomodating DST if any) in which the user(cient browser) is?
As pointed out in the comments, the output may be different depending on the users settings. A more reliable and flexibe solution is moment.js, which is a great library.
moment("2013-04-04", "YYYY-MM-DD").format("MMM Do YY"); //"Apr 4th 13"

Categories

Resources