JavaScript: Get date format from culture settings - javascript

I want to get Date format from my defined culture settings using JavaScript.
My Culture is defined in Web.config File as:
<globalization culture="en-GB" uiCulture="en-GB"/>
I am getting the defined Culture through
CultureInfo culture = System.Globalization.CultureInfo.CurrentUICulture;
and i am passing it to my client. And at Client side I am using momemt.js library to parse my json date in to actual date format.
var date = moment(JsonDate).toDate().toLocaleDateString(myCulture); //"en-GB"
and I am getting the date in the required date format as "16/07/2016"
but I also want to get this Format as dd/MM/yyyy so that I can use this culture date format in my html (for date picker).
Please let me know how can I get this date format using culture info at client side.

You can use AngularJS-s date filter
check this link please - https://docs.angularjs.org/api/ng/filter/date

You may install moment.js in your project. https://momentjs.com/
Include moment-with-locales.min.js in your HTML template.
In your JavaScript:
moment.locale(myCulture);
var date = moment(JsonDate).format('L')

Related

How to parse jqwidgets (jqx) date with format?

Basically I have date format which comes from server and which will be not known before and a date that was already formatted in this format, e.g. the format is "dd/MM/yyyy" and the date is "21/01/2018" (two strings). The format that uses jqx differs from the default JS one, as I can see.
What I need to do is to parse it to JavaScript date object. I couldn't find the answer in the official documentation.
Is there any easy was of parsing the date string if you know the date format, using JS, jqwidgets or momentjs?
Eventually, I've finished with inner jqx method that does that exact thing:
var parsedDate = $.jqx.dataFormat.parsedate(src, format);
Where format in this case is "dd/MM/yyyy" and src is "21/01/2018".
P.S. Thanks for the guy that posted anwer with the code that used $.jqx.dataFormat, that really helped me to find parsedate method.

Javascript String to date conversion without date format but passing locale

I have this user input date field, where user can enter date as string such '010117' or '01012017' which I am converting to US date standard format(MM/dd/yyyy) using JavaScript. If we have to go international we want user to input date as string as per their country standard.
Question is, Is there any library for JavaScript that we can use to pass the date as string and locale and it will convert the string to their locale standard date format.
We don't want to pass date format for each country as it will be too much of a task to do that for each country.
Thanks in advance.
There is JavaScript library MomentJS which provides very good features about date and time.
We can pass the string to MomentJS and it will give date.
By default MomentJS comes with English locale. You can change the locale by passing locale string to below function:
moment.locale(myLocale);
To get user's locale, you can do the following:
var myLocale = window.navigator.userLanguage || window.navigator.language;
Refer to the the link:
https://momentjs.com/docs/#/i18n/

Working with date formats in Java and MySQL

Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed

Compare ISO date without time stamp in javascript

I am querying data using OData, url looks like http://myurl.com/api/Customer?$filter=ResDate eq DateTime'2014-03-15T12:01:55.123'.
I'm getting date/time from jquery.datepicker instead of the static date and using moment.js to convert from DD-MM-YYYY to YYYY-MM-DDTHH:mm:ss in order to pass it to web service.
function convertDateToISOdate(date){
var newDate = moment(date,'DD-MM-YYYY').format('YYYY-MM-DDTHH:mm:ss');
return newDate;
}
Date returns from the function, is 2014-03-15T00:00:00.
Problem : 2014-03-15T12:01:55.123 is not equal to 2014-03-15T00:00:00, so there's no record selected.
What I want is , just to compare the date , not include time stamp.
Note : I can not change the format date/time at server side(Web service) because it's not belongs to me.
Any idea is much appreciated.
Your first call to moment(date,'DD-M-YYYY') is stripping the time information from the incoming data. try using moment(date) (no format) instead because momentjs recognizes your incoming date format intrinsically, without having to be told which format to use, and will correctly parse the H:M:S data, too.
MomentJS date parse information

angular.ui DatePicker how to set the Date format?

I am using the angular.ui DatePicker
http://angular-ui.github.io/bootstrap/#/datepicker
In the doc it says:
"Everything is formatted using the date filter and thus is also localized."
I am having a hard time figuring out how to use the date filter to control the date format set in the model.
Using the DatePicker I am getting a date format set in the model like this:
"2013-09-07T03:18:43.000Z"
However, I am trying to serialize with Grails, and I want my date format to be like this:
"2013-09-07T03:18:43Z"
How do I configure the DatePicker to output this format of date? Or since it is a JavaScript date, do I have to do this at the server side? Ultimately, I would like to send:
"2013-09-07T03:18:43Z"
in a JSON put to the server.
TIA
You can follow this trademark way of parsing the default ISO date format to your own format stripping out the milliseconds, in client side.
Or, in the server side (Grails) you can parse the string to your own format as below:
Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "2013-09-07T03:18:43.000Z")
.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
//prints "2013-09-07T03:18:43Z"
If you are persisting a Date instead of a String, just use Date.parse(..) (without .format).

Categories