How to convert my date to timestamp i dont know where im doing wrong.Any suggestion please ?
var dates = '27-04-2015';
var date1 = new Date(dates).getTime();
alert(date1);
Parameter you passed in the new Date('27-04-2015') is not valid. Use sepeartor '/' for change date format and send to the new Date('27/04/2015').
var dates = '27-04-2015';
var dates1 = dates.split("-");
var newDate = dates1[1]+"/"+dates1[0]+"/"+dates1[2];
alert(new Date(newDate).getTime());
Working Fiddle
Date constructor takes argument in mm-dd-yyyy format, try this
var timeStamp = function(str) {
return new Date(str.replace(/^(\d{2}\-)(\d{2}\-)(\d{4})$/,
'$2$1$3')).getTime();
};
alert(timeStamp('27-04-2015'));
Here, I had used RegExp to swap the format from dd-mm-yyyy to mm-dd-yyyy. So '27-04-2015' gives '04-27-2015'. In the expression (\d{2}\-)(\d{2}\-)(\d{4}), \d denotes integer, {} for length, () for grouping, \ to escape special characters like -.
Related
How can I convert a date value formatted as 9999-12-31T00:00:00Z to /Date(1525687010053)/ format in javascript?
I have this, but it doesn't work:
var datevalue = '9999-12-31T00:00:00Z';
var converteddate = Date.parseDate(datevalue);
I assume that you want to get the timestamp of that date. This can be achieved with the code below
var timestamp = new Date('9999-12-31T00:00:00Z').getTime()
I don't understand your question, but your code is wrong. There is no Date.parseDate() function in javascript, only Date.parse():
var datevalue = '9999-12-31T00:00:00Z';
var converteddate = Date.parse(datevalue);
document.getElementById('result').innerHTML = converteddate;
console.log(converteddate)
<p id="result"></p>
You can do your conversion in just three easy steps :
Convert your ISO 8601 string to a Date object
Use getTime to convert your Date object to a universal time timestamp
Wrap "/Date(" and ")/" around your result
Demo
function convert(iso8601string) {
return "/Date(" + (new Date(iso8601string)).getTime() + ")/";
}
console.log(convert("2011-10-05T14:48:00.000Z"));
I need to get a date in this format:
2016-07-06T10:57Z
Using this code I have been able to get a date in a format somewhat like I need:
var isoDate = new Date().toISOString();
2016-07-06T08:46:08.127Z
But is there a way I can remove the seconds and fraction of seconds from the date so it appears exactly like the date: "2016-07-06T10:57Z" ?
You will always want to remove the last 8 characters ('Z' included) thus you can use a function like slice
isoDate = isoDate.slice(0, -8); //Remove seconds + fractions + Z
isoDate += "Z"; //Add back the Z
You can use this way because the format returned by toISOString() will always be
YYYY-MM-DDTHH:mm:ss.sssZ
Please try
var isoDate = new Date().toISOString();
var pos = isoDate.lastIndexOf(':');
var datePart1 = isoDate.substring(0,pos);
var datePart2 = isoDate.substr(-1, 1);
var dateStr = datePart1+datePart2;
console.log(dateStr);
I have date from the date picker which I am accessing as -
var transdate = $j("input[name='enterdate']").val();
resulting in transdate = "6/22/2015"
I need to test if the entered date is between two dates which are defined as
startdate = '2015-02-01' and enddate = '2015-07-30'
How do I convert the transdate in yyyy-mm-dd format in the following code -
if ((new Date('transdate')>= startdate ) && (new Date('transdate') <= enddate )) {
alert("correct date entered");
}
Moment.js is a small handy library for dates that makes this easy.
moment('6/22/2015', 'M/D/YYYY')
.isBetween('2015-02-01', '2015-07-30'); // => true
Note that only the first (US format) date string needed an explicit format string supplied.
Moment can be useful for the parsing alone, eg. even if not using isBetween:
var transdate = moment('6/22/2015', 'M/D/YYYY').toDate();
var startdate = moment('2015-02-01').toDate();
var enddate = moment('2015-07-30').toDate();
transdate >= startdate && transdate <= enddate // => true
The string is not in the only format defined to be handled by the Date object. That means you have to parse it (with regular expressions or String#split or whatever), or use a library like MomentJS that will parse it for you. Once you've parsed the dates, you can compare them with < or >, etc.
Do not rely on Date to parse strings it's not defined to parse. You will run into implementations or locales where it doesn't work.
"6/22/2015" is trivial to parse with a regular expression:
var rex = /^(\d+)\/(\d+)\/(\d+)$/;
var match = rex.exec(transdate);
var dt = match ? new Date(+match[3], +match[1] - 1, +match[2]) : null;
That uses the Date constructor that accepts the parts of the date as individual numeric arguments (year, month, day). The + converts strings to numbers. The [x] are capture groups from the regex. You have to subtract one from the month because months start with 0 in JavaScript.
Similar questions have been asked many, many times but I can't seem to find a duplicate. Given the unreliability of the Date constructor to parse strings, the simplest solution is to parse the string yourself:
function parseMDY(s) {
var b = s.split(/\D/);
return new Date(b[2], b[0]-1, b[1]);
}
Here is the JSFIDDLE of you output.
Moment.js will give you good flexibility in coding.
Dont forget to add jquery and moment.js in your html
var transdate="6/22/2014";
var convertStringToValidDate = new Date(transdate);
$(document).ready(function(){
$("#selectedDate").text(transdate);
$("#validDate").text(convertStringToValidDate);
converttoformat = moment(convertStringToValidDate).format("YYYY-MM-DD");
$("#converttoyyyymmdd").text(converttoformat);
if(moment(converttoformat).isBetween('2015-02-01', '2015-07-30')){
$("#result").text("Date lies in between");
}
else{
$("#result").text("Date is out of scope");
}
});
Is there a way I could get the year, month (0 based) and day from '03/05/2013'
If so, how?
Thanks
Is there a safe way to do it that can check if it is in the correct format?
You have the Date.parse method which parses a Date string and returns its timestamp, so you can call new Date().
Something like this:
new Date(Date.parse('03/06/2013'))
Most easy is using the split() function, i think:
var date = "03/05/2013";
var dateParts = date.split("/");
var day = dateParts[0];
var month = dateParts[1];
var year = dateParts[2];
http://jsfiddle.net/s7ma2/1/
i have a string which, i want to compare with a javascript datetime object.
how to convert string "1/1/1912" into datetime using JavaScript so that i can compare like
if (EDateTime > ('1/1/1912')) {...}
You could do this simply with a split if you can guarantee the date format.
var dateArray = '1/1/1912'.split("/");
new Date(dateArray[2], dateArray[1], dateArray[0]);
var dateArray = '2012-02-17 01:10:59'.split(' ');
var year = dateArray[0].split('-');
var time = dateArray[1].split(':');
var finishDate = new Date(year[0], year[1], year[2], time[0], time[1], time[2])
How about using DateJS library?
It can convert 1/1/1912 to Monday, January 01, 1912 12:00:00 AM easily
Convert your string to timestamp with Date object.
I found something like:
function toTimestamp(year,month,day,hour,minute,second){
var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
return datum.getTime()/1000;
}
Year, month and day parts get with regular expressions.
This library may be helpful.