How to convert string 01 January 2016
to 01/01/2016
in jQuery. I am getting date from bootstrap datepicker fields by $('#datepicker').val()
method. but need to convert date to dd/mm/yyyy
format for further processing
Asked
Active
Viewed 871 times
2

Rory McCrossan
- 331,213
- 40
- 305
- 339

ajazsiddiqui
- 29
- 7
-
3FYI jQuery isn't involved in formatting dates. You need plain old JS for that. I've retagged your question for you. Note that it may also help if you stated which datepicker control you're using as it may have built-in date formatting logic. You stated it's one for Bootstrap, but there are literally hundreds. – Rory McCrossan Nov 30 '16 at 12:19
-
http://stackoverflow.com/questions/26549773/convert-date-format-in-jquery this link will help you. – SilentCoder Nov 30 '16 at 12:21
-
i am ujsing bootstrap datepicker – ajazsiddiqui Nov 30 '16 at 12:22
-
you can set output format in options of bootstrap datepicker – anshuVersatile Nov 30 '16 at 12:22
-
There is a format option in bootstrap datepicker: https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#format – Esko Nov 30 '16 at 12:23
-
https://eonasdan.github.io/bootstrap-datetimepicker/ – ajazsiddiqui Nov 30 '16 at 12:23
-
@ajazsiddiqui In that case all the information is in the documentation you just linked to: https://eonasdan.github.io/bootstrap-datetimepicker/Options/#format – Rory McCrossan Nov 30 '16 at 12:24
6 Answers
2
See this
https://jsfiddle.net/wt7wxn8x/1/
var test = new Date("01 January 2016");
console.log((test.getMonth() + 1) + '/' + test.getDate() + '/' + test.getFullYear());

Nagarjuna Reddy
- 4,083
- 14
- 41
- 85
1
You could use a library like moment.js:
moment('01 January 2016').format('DD/MM/YYYY')

user1664823
- 101
- 4
1
please add datepicker like this this in your html
<script type="text/javascript">
$('#datepicker').datepicker({
autoclose: true,
format: 'dd/mm/yyyy'
});
</script>
then get your datepicker value
$('#datepicker').val()

Rahool Dhokiya
- 135
- 6
-
1i want to appear the date in field as dd/mmm/yyyy but getting date in dd/mm/yyyy for other purpose – ajazsiddiqui Nov 30 '16 at 12:29
1
You can specify the format like so.
$('.datepicker').datepicker({
format: 'mm/dd/yyyy',
startDate: '-3d'
});

Brian McAuliffe
- 2,099
- 1
- 16
- 13
0
You have two options,
Set the
data-date-format
attribute on the input element.
or
Use the
format
config to set your desired format.
$('.datepicker').datepicker({
format: 'dd/mm/yyyy'
});
$('#submit-btn').on('click', function(e) {
$('#display-area').html($('#datepicker-1').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.min.css" rel="stylesheet"/>
<form>
<div class="form-group">
<label for="datepicker-1">Start Date</label>
<input class="form-control datepicker" id="datepicker-1" data-date-format="dd/mm/yyyy" aria-describedby="datepickerHelp">
<small id="datepickerHelp" class="form-text text-muted">Select a date.</small>
</div>
<input type="button" id="submit-btn" class="btn btn-primary" value="Show Date" />
</form>
<hr />
<strong>Date Value:</strong> <span id="display-area"></span>

Mr. Polywhirl
- 42,981
- 12
- 84
- 132
0
We can use Parse to convert string month to corresponding number. please try this one:
let str = "1 January 2016",
str2 = "1 mar 2016",
str3 = "1 au 2016";
function cDate(str) {
if(!(Date.parse(str))) {return 'error';}
let dt = str.split(' ');
let d = new Date(Date.parse(`${dt[1]} ${dt[0]}, ${dt[2]}`));
let result = `${d.getDate()}/${d.getMonth()+1}/${d.getFullYear()}`;
return result;
}
console.log(str, '==', cDate(str));
console.log(str2, '==', cDate(str2));
console.log(str3, '==', cDate(str3));

RizkiDPrast
- 1,695
- 1
- 14
- 21