I have tried to change this date in yyyy-mm-dd using
function convert(str) {
var date = new Date(str);
var mnth = ("0" + (date.getMonth() + 1)).slice(-2)
var day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
But it's giving me the error Naan in i.e 8. It's working with all other browsers.
Any one can help me in this?
Thanks
You want to go from
2014-11-03T00:00:00
to
yyyy-mm-dd
you just need
function convert(str) {
return str.split("T")[0];
}
To create a date from one with a T and dashes, try
function convert(str) {
var parts = str.split("T");
var dParts = parts[0].split("-");
var tParts = parts[1].split(":");
return new Date(dParts[0],dParts[1],dParts[2],tParts[0],tParts[1],tParts[2]);
}
var d = convert("2014-11-03T00:00:00");
alert(d);
Related
Hi I would to like to compare string example 15/01/2017 with newDate()
var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
return jQuery(this).text();
}).get();
var currentDate = new Date();
jQuery.each(dates, function (index, value) {
console.log(value);
//var parts = value.split('/');
//var mydate = new Date(parts[2],parts[0]-1,parts[1]);
//console.log("mydate is: "+mydate);
if(value < currentDate){
//do something
}
});
You just need to convert the current date to the same date format with which you are comparing.
var currentDate = new Date();
currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+(currentDate.getMonth() + 1)).slice(-2) + "/" + currentDate.getFullYear();
Now your comparison with other values in dates should work fine.
Why you use less than condition inside if statement simply do this
var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { //event date format is e.g 15/01/2017
return jQuery(this).text();
}).get();
var currentDate = new Date();
jQuery.each(dates, function (index, value) {
console.log(value);
var istrue = new Date();
currentDate = ("0"+currentDate.getDate()).slice(-2) + "/" + ("0"+
(currentDate.getMonth() + 1)).slice(-2) + "/" +
currentDate.getFullYear()=="15/01/2017";
if(istrue){
//do something
}
});
Although there are vanilla-javascript and Jquery-only based solutions, if your project is big enough I'd advice you to add moment.js to your project and use it for such comparisons.
It will make your life easier.
Check it out on the moment.js website
I want to change date format sequence from yy-mm-dd to dd-mm-yy
How can I do it in Javascript ?
I have tried
var now = new Date();
now.format("mm-dd-yy");
But its not working for me
Here is a clear and simple approach
var now = new Date();
var dd = now.getDate(); //returns date
var mm = now.getMonth()+ 1; //returns month and you need to add1 because it is array
var yy = now.getFullYear(); //returns full year
var st = dd + '-' + mm + "-" + yy; //format as string
var dateFormatted = (now.getMonth()+1)+"-"+now.getDate()+"-"+now.getFullYear();
You can use the below mentioned function to format Date
utilities.FormatDate(new Date(),"GMT", "dd/MM/yyyy")
function dateformat(date)
{
var yourdate = new Date(date);
yourdate = yourdate.getDate() + '-' + yourdate.getMonth() +1 + "-" +yourdate.getFullYear();
}
use - or / as you like
I have a date/time in an input (#myinput) using the following format:
yyyy/mm/dd hh:mm:ss
I need to compare this to todays date, so something like this...
var currentdate = new Date();
if(currentdate < $("#myinput").val()) {
alert("HELLO WORLD");
return false;
}
Any ideas?
I am using moment.js (http://momentjs.com/) for this kind of work.
Would look something like this:
var input = moment($("#myinput").val(), "YYYY/MM/DD HH:mm:ss");
if(input.diff(moment())>0){
...
If i had to use javascript only, i would use a regular expression to parse the date:
var dateString = "2012/12/05 12:00:01";
var regexp = /([0-9]{4})\/([0-9]{2})\/([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/;
var result = regexp.exec(dateString);
var date = new Date();
date.setYear(result[1]);
date.setMonth(result[2]-1);
date.setDate(result[3]);
date.setHours(result[4]);
date.setMinutes(result[5]);
date.setSeconds(result[6]);
if(new Date()<date){
...
You can use the Date.Parse method (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse) to compare the date inside your input against the current date or other dates in any kind of correct date format.
Ex:
var testDate = $('#myinput').val();
var d = new Date;
if (Date.parse(d)<Date.parse(testDate)) {
}
else{
}
Here is a fiddle: http://jsfiddle.net/uLybp/3/
use this function
//get full data now
function dateNow(){
var now = new Date(Date.now());
var dd=now.getDay() + "-" + (now.getMonth()+1) + "-" + now.getFullYear()+" ";
dd += now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
return dd;
}
example
var d=dateNow();
console.log(d); // 5-2-2021 1:35:24
I pick this date from a textbox and i would like to format to this format: yyyy-MM-dd
So from dd/MM/yyyy to yyyy-MM-dd
var startDate = document.getElementById('ctl00_PlaceHolderMain_ctl00_Date').value;
var s = new Date(startDate);
alert(startDate); //which prints out 7/03/2012
//when i use the below to try and format it to : yyyy-MM-dd which is what i want
var scurr_date = s.getDate();
var scurr_month = s.getMonth();
scurr_month++;
var scurr_year = s.getFullYear();
For some reason i get:
var fstartdate = scurr_year + "-" + scurr_month + "-" + scurr_date;
//Output:2012-7-3
instead of : 2012-3-7
also fi i pick a date like 31/12/2011
i get : 2013-7-12
Any ideas what to do.I kind of notice if i use US like 03/07/2012 it kind os works ok.
Thank in advance
You said you want to convert from "dd/MM/yyyy to yyyy-MM-dd". JavaScript's Date constructor will always take the first two digits as a month.
Some regex might help you here:
function fix_date (str) {
var re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
str = str.replace(re, function (p1, p2, p3, p4) {
return p4 + '/' + p3 + '/' + p2;
});
return str;
}
var start_date = '7/03/2012';
var new_date = fix_date(start_date);
console.log(new_date); // 2012/03/7
http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3
and this
http://www.elated.com/articles/working-with-dates/
Basically, you have 3 methods and you have to combine the strings for yourself:
getDate(): Returns the date
getMonth(): Returns the month
getFullYear(): Returns the year
<script type="text/javascript">
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
</script>
check this answer link
Suppose I receive two dates from the datepicker plugin in format DD/MM/YYYY
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
/*this dates are results form datepicker*/
if(process(date2) > process(date1)){
alert(date2 + 'is later than ' + date1);
}
What should this function look like?
function process(date){
var date;
// Do something
return date;
}
Split on the "/" and use the Date constructor.
function process(date){
var parts = date.split("/");
return new Date(parts[2], parts[1] - 1, parts[0]);
}
It could be more easier:
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
if ($.datepicker.parseDate('dd/mm/yy', date2) > $.datepicker.parseDate('dd/mm/yy', date1)) {
alert(date2 + 'is later than ' + date1);
}
For more details check this out. Thanks.
function process(date){
var parts = date.split("/");
var date = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
return date.getTime();
}