i have a date with this format" 20/02/2018 14:40:00 CET" how can i convert it with the format ISOString, i tried this code but i haven't any result !!
function myFunction() {
var d = '20/02/2018 14:40:00 CET';
var n = d.toISOString();
document.getElementById("demo").innerHTML = n;
}
You need to create a new date instance before try use toISOString method, however correct date format is mm/dd/yyyy so try this :
var d = new Date("02/20/2018 14:40:00");
var n = d.toISOString();
console.log(n);
You need to split and re-assemble since European date format (20/02) is not parse-able
var d = '20/02/2018 14:40:00 CET',
parts = d.split(" "),
time = parts[1].split(":"),
dParts = parts[0].split("/");
+time[0]--; // CET is +1 - for more https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
var n = dParts[2] + "-" +
dParts[1] + "-" +
dParts[0] + "T" +
time[0] + ":" +
time[1] + ":" +
time[2] + ".000Z";
console.log(n);
Related
So what I have is a time string which shows the time as h:m:s.ms
But the problem is that I want to covert them to timestamp values it shows NaN values.
I am using Date.parse() to convert the time into timestamp.
Here is the code that I have tried.
var date;
function myFunction() {
var d = new Date();
var h = addZero(d.getHours(), 2);
var m = addZero(d.getMinutes(), 2);
var s = addZero(d.getSeconds(), 2);
var ms = addZero(d.getMilliseconds(), 3);
var maindate = h + ":" + m + ":" + s + "." + ms ;
var datestring = Date.parse(maindate)
var data = Math.random(0,1);
console.log("Date : ", maindate) ;
console.log("Data : ", data);
}
myFunction();
You can see the date and data in the console window.
the date variable here shows NaN Value.
Please tell me what I am doing wrong.
If you want a timestamp you need a full time with day, month and year
var date;
function myFunction() {
var d = new Date();
var h = addZero(d.getHours(), 2);
var m = addZero(d.getMinutes(), 2);
var s = addZero(d.getSeconds(), 2);
var ms = addZero(d.getMilliseconds(), 3);
var day = d.getDate();
var month = d.getMonth() + 1; // getMonth returns an integer between 0 and 11
var year = d.getFullYear();
var maindate = `${day}-${month}-${year} ${h}:${m}:${s}.${ms}`;
var datestring = Date.parse(maindate)
console.log("Data : ", datestring);
}
myFunction();
Parsing dates is a pain in JavaScript as there's no extensive native support. However you could do something like the following by relying on the Date(year, month, day [, hour, minute, second, millisecond]) constructor signature of the Date object.
var dateString = '17-09-2013 10:08',
dateTimeParts = dateString.split(' '),
timeParts = dateTimeParts[1].split(':'),
dateParts = dateTimeParts[0].split('-'),
date;
date = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);
console.log(date.getTime()); //1379426880000
console.log(date); //Tue Sep 17 2013 10:08:00 GMT-0400
You could also use a regular expression with capturing groups to parse the date string in one line.
var dateParts = '17-09-2013 10:08'.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/);
console.log(dateParts); // ["17-09-2013 10:08", "17", "09", "2013", "10", "08"]
As I want to get the timestamp in result. I got my sholution of the above question that I posted
Here is the Final code which is giving me the expected result.
function addZero(x,n) {
while (x.toString().length < n) {
x = "0" + x;
}
return x;
}
var date;
function myFunction() {
var d = new Date();
var day = d.getDate();
var month = d.getMonth() + 1; // Since getMonth() returns month from 0-11 not 1-12
var year = d.getFullYear();
var h = addZero(d.getHours(), 2);
var m = addZero(d.getMinutes(), 2);
var s = addZero(d.getSeconds(), 2);
var ms = addZero(d.getMilliseconds(), 3);
var maindate = year +"-" + day + "-" + month +" "+ h + ":" + m + ":" + s + "." + ms ;
var datestring = Date.parse(maindate)
var data = Math.random(0,1);
console.log("Date : ", datestring) ;
console.log("Data : ", data);
}
myFunction();
I had to include the day month and year value also. WHich I updated in the Answer. rest of the code works fine.
For this, you don't need your addZero() function any more and it's unnecessary to delacre var date; globally.
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var ms = d.getMilliseconds();
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var maindate = day + '-' + month + '-' + year + ' ' + h + ':' + m + ':' + s + '.' + ms;
var datestring = Date.parse(maindate);
console.log("Data : ", datestring);
Take a look at momentjs.com, maybe this could be a clean and simple solution for you too - depending on your environment.
I'm trying to convert this 13 digit Unix timestamp (1563398686957) to YYYYMMDD format using Javascript. How can I do this?
I have divided the 1563398686957/1000 and tried to get the first 10 digits but converting from Number to String and back gives me an error and is not there right way to do it if I am looping for many timestamps.
var newCreateDate = 1563398686957 / 1000;
var newTimestamp = Array();
for (let i = 0; i < newCreateDate.length; i++) {
temp_timestamp = String(newCreateDate[i].slice(0, 9));
newTimestamp.push(Number(temp_timestamp));
}
You can pass timestamp into Date:
var unixts = 1563398686957;
var date = new Date(unixts);
var fdate = date.getFullYear() + '/' + ("0" + (date.getMonth() + 1)).slice(-2) + '/' + ("0" + date.getDate()).slice(-2);
console.log(fdate);
new Date(1563398686957).toISOString().substr(0, 10)
Will give you the date in this form: 2019-07-17
new Date(1563398686957).toISOString().substr(0, 10).replace(/-/g, '/')
Will change the dashes to slashes, if you prefer, and...
new Date(1563398686957).toISOString().substr(0, 10).replace(/-/g, '')
Would give you 20190717.
I am trying to split a date using the following piece of JavaScript
var dSplit = getDate.split("/");
var newDate = dSplit[2] + "-" + dSplit[0] + "-" + dSplit[1];
I get the following output
2014 12:00:00 AM-11-25
The output i require is
2014-11-25 12:00:00 AM
Please Help.
One possible approach:
var getDate = '11/25/2014 12:00:00 AM';
var newDate = getDate.replace(/^\S+/, function(date) {
var d = date.split('/');
return d[2] + '-' + d[0] + '-' + d[1];
});
// 2014-11-25 12:00:00 AM
This approach allows to process both datetime strings (similar to '11/25/2014 12:00:00 AM', like in your answer) and date strings (like '11/25/2014'). The key here is processing only first sequence of non-whitespace characters in the string.
You may format the date to string as you want, using the next function:
function formatDate(date) {
var ans = date.getFullYear();
ans += "-" + (date.getMonth()+1);
ans += "-" + date.getDay();
ans += " " + date.getHours();
ans += ":" + date.getMinutes();
document.write (ans);
}
This way, even if the user's browser converts date to string on different order (longer format etc.) you have full control on the output string.
This may be helpfull,pass$val alone in function
var dateString=$val.split(" ");
var dateformat=dateString[0].split("-");
var dateVal= dateformat[0] + "/" + dateformat[1] + "/" + dateformat[2];
$.date = function(dateObject) {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = year + "-" + month + "-" + day;
return date;
};
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 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