I have the following code that converts a date input into a roman date:
function romanize (num) {
if (!+num)
return false;
var digits = String(+num).split(""),
key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
"","I","II","III","IV","V","VI","VII","VIII","IX"],
roman = "",
i = 3;
while (i--)
roman = (key[+digits.pop() + (i * 10)] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
}
$(document).on("change",'#date', function() {
var date = new Date($('#date').val());
day = date.getDate();
month = date.getMonth() + 1;
year = date.getFullYear();
var strRomanDate = romanize(month) + " " + romanize(day) + " " + romanize(year);
$('#romandate .date').html(strRomanDate);
});
Now, this is working fine for some dates, for ex:
10/04/2018 --> X IV MMXVIII
But when I want to select a day after 12, so 13, 14,... It returns false false false.
Anyone have an idea what I should change in my code to make it work for every date?
Related
The code that generates my number:
<input type="radio" class="alege" name="uni_code_type" id="alegeT" value="T" onclick="getUniCode()">
<label for="alegeT" class="control-label">T</label>
<input type="radio" class="alege" name="uni_code_type" id="alegeP" value="P" onclick="getUniCode()">
<label for="alegeP" class="control-label">P</label>
<input id="uni_code" name="uni_code" type="text" class="form-control">
and here is the corresponding JS:
var seq = 0;
// var d = new Date();
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = day + "/" + month + "/" + year;
function getUniCode() {
// Increment the value
seq += 1
// The string value that will be displayed
var value = document.querySelector('input[type="radio"].alege:checked').value;
// If sequence is less than 10, prefix with 000
if (seq < 10) {
value += '00' + seq + "-" + newdate;
}
// If sequence is less than 100, prefix with 00
else if (seq < 100) {
value += '0' + seq + "-" + newdate;
}
// If sequence is less than 1000, prefix with 0
else if (seq < 1000) {
value += '' + seq + "-" + newdate;;
}
// Otherwise, just use the value directly
else {
value += seq;
}
// Display the formatted value (ie prefixed with 0's)
document.getElementById("uni_code").value = value;
}
For now, if I select T, it generates a code like T001-6 / 4/2020 and if I select P and then I select T, this appears: P002-6 / 4/2020. I would like it after I press T and then press P to appear also at P all from 1 not from the number generated when I press T! Thank you!
I solve it thanks hehe :D ` var seq = 0;
// var d = new Date();
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = day + "/" + month + "/" + year;
function getUniCode() {
// Increment the value
seq += 1
// The string value that will be displayed
var value = document.querySelector('input[type="radio"].alege:checked').value;
// If sequence is less than 10, prefix with 000
if (seq < 10) {
value += '00' + seq + "-" + newdate;
}
// If sequence is less than 100, prefix with 00
else if (seq < 100) {
value += '0' + seq + "-" + newdate;
}
// If sequence is less than 1000, prefix with 0
else if (seq < 1000) {
value += '' + seq + "-" + newdate;;
}
// Otherwise, just use the value directly
else {
value += seq;
}
// Display the formatted value (ie prefixed with 0's)
document.getElementById("uni_code").value = value;
}
//
var seqe = 0;
// var d = new Date();
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = day + "/" + month + "/" + year;
function getUniCodee() {
// Increment the value
seqe += 1
// The string value that will be displayed
var value = document.querySelector('input[type="radio"].alege:checked').value;
// If sequence is less than 10, prefix with 000
if (seqe < 10) {
value += '00' + seqe + "-" + newdate;
}
// If sequence is less than 100, prefix with 00
else if (seqe < 100) {
value += '0' + seqe + "-" + newdate;
}
// If sequence is less than 1000, prefix with 0
else if (seqe < 1000) {
value += '' + seqe + "-" + newdate;;
}
// Otherwise, just use the value directly
else {
value += seqe;
}
// Display the formatted value (ie prefixed with 0's)
document.getElementById("uni_code").value = value;
}`
i did this and now it works as i want :D
This is my code what i am trying is to make a datepicker to select number of days selected without Saturday and Friday it is working correctly but when I select more than one weekend (2 Friday or 2 sat) and the last day is weekend it escapes the last weekend like if day 30 is Saturday and i select from day 1 to day 30 it gives me 23 how ever if I select 1 to 28 it gives me 22.
var date = new Date();
var tomorrow = new Date();
tomorrow.setDate(date.getDate() + 1);
$('#picker').daterangepicker({
"linkedCalendars": true,
"showCustomRangeLabel": false,
"startDate": date.getDate(),
"endDate": tomorrow,
"cancelClass": "btn-danger",
"minDate": date.getDate(),
"opens": "center",
"autoApply": true
}, function(start, end) {
function parseDate(str) {
var mdy = str.split('-');
var x = new Date("'" + mdy[0] + "-" + mdy[1] + "-" + mdy[2]);
return x;
}
var between = [];
function datediff(first, second) {
var currentDate = new Date(first);
between = [];
while (currentDate <= second) {
between.push("'" + currentDate.getFullYear() + "-" + (currentDate.getMonth() + 1) + "-" + currentDate.getDate() + "'");
currentDate.setDate(currentDate.getDate() + 1);
}
//return Math.round((second-first)/(1000*60*60*24))+1;
}
datediff(parseDate(start.format('YYYY-MM-DD')), parseDate(end.format('YYYY-MM-DD')));
var s = "",
n = "",
fri = "",
sat = "",
i;
var number = between.length;
function getweekend() {
for (i = 0; i <= number + 1; i++) {
s = new Date(between[i]);
n = s.getDay();
fri = (n === 5);
sat = (n === 6);
if (fri) {
console.log("fri");
number = number - 1;
continue;
} else if (sat) {
console.log("sat");
number = number - 1;
continue;
} else {
console.log(n);
continue;
}
}
console.log("after minus:" + number);
}
console.log("plus minus:" + number);
getweekend();
});
I have a formatted date as a string '021519' using javascript which return NAN on display in html.
Note I have a xslt using the javascript.
var newDate = '';
var formatedDate = new Date(date);
var year = formatedDate.getFullYear().toString();
var month = (1 + formatedDate.getMonth()).toString();
if(parseInt(month) < 10)
{
month = "0" + month;
}
var day = formatedDate.getDate().toString();
if(dateFormat == '1')
{
newDate = month + day + year.substr(2,4);
}
else
{
newDate = month + day + year;
}
var newLeftStart3 = parseInt(startPosition) - 1;
var newLeftEnd3 = newLeftStart + newDate.length;
var newRightStart3 = parseInt(endPosition) - newDate.length;
var newRightEnd3 = newRightStart + newDate.length;
if(alignment == '1')
{
addendaSpace = addendaSpace.substr(0, newLeftStart3) + newDate + addendaSpace.substr(newLeftEnd3);
}
if(alignment == '2')
{
addendaSpace = addendaSpace.substr(0, newRightStart3) + newDate + addendaSpace.substr(newRightEnd3);
}
newDate is displaying as NaN i hope this code helps.
I usually format dates in javascript this way:
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1; //Months are zero based
var year = d.getFullYear();
console.log(date + "-" + month + "-" + year);
I got this from another stack question
incr_date(date_str){
let parts = date_str.split("-");
let dt = new Date(
parseInt(parts[0], 10), // year
parseInt(parts[1], 10) - 1, // month (starts with 0)
parseInt(parts[2], 10) // date
);
dt.setDate(dt.getDate() + 1);
parts[0] = "" + dt.getFullYear();
parts[1] = "" + (dt.getMonth() + 1);
if (parts[1].length < 2) {
parts[1] = "0" + parts[1];
}
parts[2] = "" + dt.getDate();
if (parts[2].length < 2) {
parts[2] = "0" + parts[2];
}
return parts.join("-");
}
It works but how can I convert this function to decrement the date instead of increment?
I'm doing this on a react native component so I dont want to import any javascript libraries like moment.js
function dateAdd(dte){
var date = new Date(dte);
date.setDate(date.getDate() + 1);
console.log("add one day= "+date)
}
function datesub(dte){
var date = new Date(dte);
date.setDate(date.getDate() - 1);
console.log("minus one day = "+ date)
}
dateAdd("01-01-2017")
datesub("01-01-2017")
I'd convert the string to Javascript understandable format, increment a day and convert it back to user understandable format. I'm using the flag(Boolean) to determine weather to Increment the date and vice versa.
var convertDate = function(dt, flag) {
var dateArr = dt.split('-');
var tempDate = new Date();
var mm = dateArr[1] - 1; //Javascript considers 0 as Jan
tempDate.setFullYear(dateArr[0]);
tempDate.setMonth(mm);
tempDate.setDate(dateArr[2]);
if (flag) {
tempDate.setDate(tempDate.getDate(dateArr[2]) + 1);//Add's one day
} else {
tempDate.setDate(tempDate.getDate(dateArr[2]) - 1);//Sub's one day
}
var userFriendlyMonth = (Number(tempDate.getMonth()) + 1); //user considers 1 as Jan
return tempDate.getFullYear() + '-' + userFriendlyMonth + '-' + tempDate.getDate();
}
document.getElementById("increment").innerHTML = convertDate('2018-11-30', true);
document.getElementById("decrement").innerHTML = convertDate('2018-11-30', false);
<div>Increment: <span id="increment"></span></div>
<div>Decrement: <span id="decrement"></span></div>
I want to get all dates in between 2 dates. So here I have mentioned statdate is date and end date is weekdate. In between 2 dates I want all dates.
Actully I am getting all dates But Not proper Format ,what i want in this format DD/MM/YY.
Now I am Getting in default Format (Sat Jun 09 2007 17:46:21)
$(document).ready(function () {
$("#day").click(function () {
startJsonSession();
return false;
});
function startJsonSession() {
var inputdate = $('#inputdate').val();
//alert("Input Date!!!" + inputdate );
var d = new Date(inputdate);
var nowMS = d.getTime(); // get # milliseconds for today
//alert(nowMS);
var week = 1000 * 60 * 60 * 24 * 7; // milliseconds in one week
//alert(week);
var oneWeekFromNow = new Date(nowMS + week);
//alert("oneWeekFromNow!!!" + oneWeekFromNow);
var fromdate = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (fromdate < 10) {
fromdate = "0" + fromdate;
}
if (month < 10) {
month = "0" + month;
}
//var date = fromdate + "/" + month + "/" + year;
var date = year + "/" + month + "/" + fromdate;
alert("InputDate!!!!" + date);
//var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
var weekdate = year + "/" + month + "/" + oneWeekFromNow.getDate();
alert("weekdate!!!" + weekdate);
var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));
var tomorrowdate = tomorrow.getDate();
var month1 = tomorrow.getMonth() + 1;
var year1 = tomorrow.getFullYear();
if (tomorrowdate < 10) {
tomorrowdate = "0" + tomorrowdate;
}
if (month1 < 10) {
month1 = "0" + month1;
}
//var nextday = tomorrowdate + "/" + month1 + "/" + year1;
var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
alert("tomorrow!!!!" + nextday);
var d1 = new Date(date);
alert("D1!!!!!" + d1.);
var d2 = new Date(weekdate);
var aDates = [];
do {
aDates.push(d1.toString());
d1.setDate(d1.getDate() + 1);
}
while (d1 <= d2);
alert("Dates!!!" + aDates);
//alert(aDates.join("\n"));
}
});
You can do it in this way
$("#getDate").click(function () {
var start = $("#startdate").datepicker("getDate"),
end = $("#enddate").datepicker("getDate");
currentDate = new Date(start),
between = [];
while (currentDate < end) {
between.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
for (var i = 0; i < between.length; i++) {
var date = $.datepicker.formatDate('dd/mm/yy', new Date(between[i]));
between[i] = date;
}
console.log(between)
})
Here 'between' is the array which contains all your required Date
SEE DEMO HERE
alert("Dates!!!" + aDates.getDate()+"/"+ (aDates.getMonth()+1)+"/"+ aDates.getFullYear());
You seem to want to get a array of date strings in d/m/y format given an input string in the same format. The following functions will do that.
// Parse a string in dmy format
// return a date object, NaN or undefined
function parseDMY(s) {
var b = s.match(/\d+/g);
if (b) {
return new Date(b[2], --b[1], b[0]);
}
}
// Given a date object, return a string in dd/mm/yyyy format
function formatDMY(date) {
function z(n){return (n<10? '0' : '') + n;}
return z(date.getDate()) + '/' + z(date.getMonth() + 1) + '/' + date.getFullYear();
}
function getWeekDates(s) {
var d = parseDMY(s);
var dates = [];
if (d) {
for (var i=0; i<7; i++) {
dates.push(formatDMY(d));
d.setDate(d.getDate() + 1);
}
return dates;
}
}
console.log(getWeekDates('7/7/2014').join());
// 07/07/2014,08/07/2014,09/07/2014,10/07/2014,11/07/2014,12/07/2014,13/07/2014
Note that adding 1 day to a date is preferred over adding milliseconds as it allows the Date object to take account of daylight saving changes that might be involved.