How to apply moment.js to existing variables in html - javascript

Is there a way to form the current date and time into a moment.js using these variables?
var x = new Date(document.lastModified);
var y = new Date();
document.writeln('The last modified date is: ' + x + ' and Date is: ' + y);

const last = moment(document.lastModified, "MM/DD/YYYY");
const now = moment();
document.write('The last modified date is: ' + last.format("YYYY-MM-DD") + ' and Date is: ' + now.format("YYYY-MM-DD"));
<script src="https://momentjs.com/downloads/moment.js"></script>

You can use n.toISOString(); to convert your date to ISO format and then parse it in momentjs.
var date = moment(n.toISOString()).format('DD-MM-YYYY');
Example:
var n = new Date();
n.toISOString();
var date = moment(n.toISOString()).format('DD-MM-YYYY');
console.log(date);

Related

javascript function to accept multiple date formats

Users want to use the following formats to enter dates:-
mm-dd-yyyy OR yyyy-mm-dd OR m-d-yy OR m-d-yyyy OR mm/dd/yyyy OR m/d/yy.
My plan is to capture whatever they enter and convert it to yyyy-mm-dd because that is the format that the date field value must be submitted. They have refused to use a calendar . I have tried the following JS function without any success. Any ideas?
var value = ctrl.getValue();
var date_input = new Date(value);
var day = date_input.getDay();
var month = date_input.getMonth() + 1;
var year = date_input.getFullYear();
var yyyy_MM_dd = year + "-" + month + "-" + day;
return yyyy_MM_dd
Because its wrong to use var day = date_input.getDay();
use it like this
var day = date_input.getDate();
function getDateFormat(value){
var date_input = new Date(value);
var day = date_input.getDate();
var month = date_input.getMonth() + 1;
var year = date_input.getFullYear();
var yyyy_MM_dd = year + "-" + month + "-" + day;
return yyyy_MM_dd;
}
console.log(getDateFormat("2017-12-30"));
console.log(getDateFormat("12-30-2017"));
.getDay is giving you days of weeks
Also be sure to use date format accepted by Date

convert string to Date Javascript

Trying to convert a string to Javascript Date.
My string:
var fullDate = this.date + " " + this.time + ":00";
gives an output: 24/12/2016 02:00:00
but trying to convert it in js Date object like:
var k = new Date(fullDate);
gives the output: Invalid Date
or even: var k = Date.parse(fullDate);
gives the output: NaN
Any help is welcome.
I was following instructions from http://www.datejs.com/ so far
Your format is invalid. Valid format is month/day/year, so try:
var fullDate = "12/24/2016 02:00:00";
var k = new Date(fullDate);
Hope I could help!
I've just posted an answer/question for this.
Here's my answer
In summary you need to format the string to a standardized format first and then you can use that string with the new Date(string) method. This work for many browsers (IE9+, Chrome and Firefox).
stringFormat = moment(dateObject).format("YYYY-MM-DDTHH:mm:ss");
date = new Date(stringFormat);
That's happening because you don't provide a valid date format for the new Date. So the date format is incorrect. The default date format in JavaScript is dd/mm/yyyy.
The date should be like(mm/dd/yyyy):
12/24/2015
And then you can use var k = new Date(fullDate);
A pure javascript solution to format the date(without moment.js):
var fullDate = "24/12/2016 02:00:00";
fullDate = fullDate.split(' ');
var date = fullDate[0].split(/\//);
var time = fullDate[1];
var newDate = date[1] + '/' + date[0] + '/' + date[2] + ' ' + time;
var k = new Date(newDate);

Javascript format date

I have a date string which coming from the db as follows
/Date(1469167371657)/
Is there any way to convert this date to following format using javascript
MM/DD/YYYY HH:MM
I've searched a lot but unble to find a solution
In plain javascript you have to write your own function for string format a date, for example for your string format:
var date = new Date(1469167371657);
function stringDate(date) {
var mm = date.getMonth()+1;
mm = (mm<10?"0"+mm:mm);
var dd = date.getDate();
dd = (dd<10?"0"+dd:dd);
var hh = date.getHours();
hh = (hh<10?"0"+hh:hh);
var min = date.getMinutes();
min = (min<10?"0"+min:min);
return mm+'/'+dd+'/'+date.getFullYear()+" "+hh+":"+min;
}
console.log(stringDate(date));
drier code version
var date = new Date(1469167371657);
function stringDate(date) {
return ("0" + (date.getMonth() + 1)).slice(-2)+'/'
+("0" + date.getDate()).slice(-2)+'/'
+date.getFullYear()+" "
+("0" + date.getHours()).slice(-2)+':'
+("0" + date.getMinutes()).slice(-2)
}
console.log(stringDate(date));
with pure js you can do the folowing
var d = new Date();
console.log(d.getMonth() + 1 + "/" + d.getDate() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes())
You can use - http://momentjs.com/ and have it done like:
moment(1469167371657).format('MM/DD/YYYY HH:MM')
You can do this with the following steps:
1) convert the timestamp to a date object.
var timestamp = "/Date(1469167371657)/"; // However you want to save whatever comes from your database
timestamp = timestamp.substr(timestamp.indexOf("(")+1); // gives 1469167371657)/
timestamp = timestamp.substr(0,timestamp.indexOf(")")); // gives 1469167371657
var d = new Date(timestamp);
2) set it to your format
function leadZero(i) {if(i < 10) {return "0"+i;} return i;} // Simple function to convert 5 to 05 e.g.
var time = leadZero(d.getMonth()+1)+"/"+leadZero(d.getDate())+"/"+d.getFullYear()+" "+leadZero(d.getHours())+":"+leadZero(d.getMinutes());
alert(time);
Note: the date / timestamp you provided is too high for javascript to understand, so this example will not work correclty
I believe that number is milliseconds so to convert it to date, you would do this:
var time = new Date().getTime();
var date = new Date(time);
alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800 (PST)
var time=1469167371657;
var date = new Date(time);
alert(date.toString());

Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript

I'm trying to convert date format (DD-MM-YYYY) to (YYYY-MM-DD).i use this javascript code.it's doesn't work.
function calbill()
{
var edate=document.getElementById("edate").value; //03-11-2014
var myDate = new Date(edate);
console.log(myDate);
var d = myDate.getDate();
var m = myDate.getMonth();
m += 1;
var y = myDate.getFullYear();
var newdate=(y+ "-" + m + "-" + d);
alert (""+newdate); //It's display "NaN-NaN-NaN"
}
This should do the magic
var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");
Don't use the Date constructor to parse strings, it's extremely unreliable. If you just want to reformat a DD-MM-YYYY string to YYYY-MM-DD then just do that:
function reformatDateString(s) {
var b = s.split(/\D/);
return b.reverse().join('-');
}
console.log(reformatDateString('25-12-2014')); // 2014-12-25
You can use the following to convert DD-MM-YYYY to YYYY-MM-DD format using JavaScript:
var date = "24/09/2018";
date = date.split("/").reverse().join("/");
var date2 = "24-09-2018";
date2 = date.split("-").reverse().join("-");
console.log(date); //print "2018/09/24"
console.log(date2); //print "2018-09-24"
You just need to use return newdate:
function calbill()
{
var edate=document.getElementById("edate").value;
var myDate = new Date(edate);
console.log(myDate);
var d = myDate.getDate();
var m = myDate.getMonth();
m += 1;
var y = myDate.getFullYear();
var newdate=(y+ "-" + m + "-" + d);
return newdate;
}
demo
But I would simply recommend you to use like #Ehsan answered for you.
First yo have to add a moment js cdn which is easily available at here
then follow this code
moment(moment('13-01-2020', 'DD-MM-YYYY')).format('YYYY-MM-DD');
// will return 2020-01-13

Convert date in javascript

How can I convert this date:
2011-11-02T10:41:43+0000
Into this using JavaScript:
02/11
Thankful for all help!
If that date is a string, a simple RegEx can offer the desired results:
var date = "2011-11-02T10:41:43+0000";
date = date.match(/-(\d{1,2})-(\d{1,2})T/);
date = date[2] + "/" + date[1]; // date = "02/11"
var d = new Date('2011-11-02T10:41:43+0000'),
dateString = d.getDate()+'/'+(d.getMonth()+1);
console.log(dateString); // 2/11
You could do this
var d = new Date('2011-11-02T10:41:43+0000');
var e = d.getUTCDate() + '/' + (d.getUTCMonth() + 1);
alert(e);
Example: http://jsfiddle.net/pRbZ2/
Assuming that the 11 you want is the month and not the year

Categories

Resources