This is my JS function for displaying the date in the browser, but the format is changing according the browser. Means when i open my project on chrome then format is 4/30/2015, and when open on IE then 30 april, 2015. How can i fix this?
document.getElementById(lblClock).innerHTML = nd.toLocaleString();
I am using this. How can i do this without using Date.js and moment.js? Please help me
Nothing Special about this previously i was using this which convert the locale date into a string.
document.getElementById(lblClock).innerHTML = nd.toLocaleString();
Just give your specific format in which you want to see your date just like this.
document.getElementById(lblClock).innerHTML = nd.format('dddd,MMM dd,yyyy,HH:mm:ss');
It will work on any browser or any version.
Try to use date format lib from http://blog.stevenlevithan.com/archives/date-time-format
or try to find something similar
Related
I want do display Dates from different time-zone (PDT, IST, JST and AEST) on single HTML page. I'm not very proficient in JavaScript learning currently. Any help is really appreciated.
In case you want to display current date, the javascript Date() will automatically get the date based on local timezone.
var date = new Date();
You can also refer to all the methods javascript date supports here -> https://www.w3schools.com/jsref/jsref_obj_date.asp
Best way to handle dates is by using moment library
https://momentjs.com/timezone/
Using the code below, I am getting a Date from my database (SQL) and displaying it in a datapicker form field. The date displays fine for me but if I change my time zone (EST) in my system to one that is behind mine, the field will display the date as the day before. Does anyone know why this is occurring and how to fix it?
var NetNewBusinessDate = moment(model.NetNewBusinessDate).format("M/D/YYYY"));
model.NetNewBusinessDate == "/Date(1494561600000)/"
Found the answer here. Moment.js local relative time
The issue had to do with UTC conversion and Moment.js has an extension method that handles that. I just used moment.utc instead of moment and it worked like a charm!
Given a text field, I want to have a suitable placeholder. A typical placeholder will be something like: "mm/dd/yyyy".
However, I would like to use locale-aware dates using moment.js.
This means that I will be specifying "l" as the moment.js date format, howe do I determine the date format that moment.js will be using in this case?
The user will not understand what "l" means, so using this value in the placeholder text makes very little sense.
Specifically, I am hoping to be able to access something like moment's internal "defaultLongDateFormat". (Though that is merely a default - moment.js probably updates it or has some other mapping at runtime for locale-aware date formats - I would like to access that mapping.)
EDIT:
There are multiple downvotes (who aren't explaining why they're downvoting it).
I think this is because they arent' understanding the question, so here are some examples:
I want a function such that:
getFormat("l") -> "mm/dd/yyyy", or equivalent for the US locales.
getFormat("l") -> "dd/mm/yyyy", or equivalent, for the AU locales.
I do not want to format a given date, or to parse a given date - I merely want to determine it's user-friendly format given an arbitruary moment.js format, specifically, for 'l'.
I don't think it's exposed nicely, but if the browser has its language configured correctly you can do something like this:
var lang = navigator.languages ? navigator.languages : navigator.language;
moment().locale(lang).localeData()._longDateFormat['L']
Languages behave slightly differently depending on which browser you're using, so don't know how reliable this is.
Follow up to Adam R's answer:
Seems to have been exposed by now:
localeData.longDateFormat(dateFormat);
returns the full format of abbreviated date-time formats LT, L, LL and so on
(source: http://momentjs.com/docs/)
Get the currently used locale data by moment.localeData()
I'm trying to convert a simple string to a unix timestamp using moment.js
moment('2014-01-14 07:25 PM').unix();
moment('2014-01-14 07:25 AM').unix();
The problem is I get the same result with AM or PM in that string.
1389684300
What gives?
The docs make no mention that your specified format is guaranteed to be correctly recognized. It says
Warning: Browser support for this is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.`
You should probably explicitly specify a format in the second argument.
This should work (JSFiddle):
moment('2014-01-14 07:25 PM', 'YYYY-MM-DD hh:mm A').unix();
It seems to have been a bug in version 2.0.0. Updating to 2.5.0 and the problem is fixed.
I'm using HTML5 input type=date field and because some browsers still don't support this feature, I would like to create error validation message for browsers that display just normal text field instead of date field. This error message should look like
Please enter date in format: ...
But I need to find the correct format, that the browser is set to. Is there any php/js/jQuery way how to find out this?
Thanks to #Jose Vega's answer I was able to find very easy way how to do it.
var now=new Date(2013,11,31);
var str=now.toLocaleDateString();
str=str.replace("31","dd");
str=str.replace("12","mm");
str=str.replace("2013","yyyy");
Error message:
"Please enter date in format:" + str
I have used the globalize library to do something similar. I don't know if its functionality has what you are looking for, but I know it does a good job handling different cultures. I use the window.navigator.userLanguage to determine culture and then feed it to the globalize library to do its thing. I've had success when dealing with currency and numbers.
EDIT: See if this helps:
var now=new Date();
alert(now.toLocaleString());
taken from How format JavaScript Date with regard to the browser culture?
Reference Displaying proper date format depending on culture
Why not to allow users to enter date in any format they like.
E.g. i am tourist in the USA and the browser says me to use format mm/dd/YYYY instead of my native dd.mm.YYYY.
Just get any date entered by users and parse it with Date.parse()