Hi i am trying to convert dd/mmm/yyyy into a binary/epoch date.
example:
06/mar/2015
into
1425600000000
i build up my date
var Buildup = day + slash + month + slash + year;
but now i dont have a clue how to do it i have tried something like this:
var formattedDays = Buildup.split(" ")[0].split("-");
var epoch = new Date(formattedDays[0], formattedDays[1] - 1, formattedDays[2]).getSeconds;
var epochStart = new Date(formattedDays[0], formattedDays[1] - 1,formattedDays[2],formattedTime[0],formattedTime[1],formattedTime[2],0).getTime()/1000;
but no look?
like this :
var d = new Date('06/03/2015');
var n = d.getTime();
Date Documentation
GetTime Documentation
Related
I have tried this below
var dt = new Date();
utc = dt.toISOString();
console.log(utc)
which gives the result as such 2021-04-22T04:32:33.676Z
but I want this UTC DateTime in exactly yyyyMMdd’T’HHmmss’Z format...
As per my understanding, you don't want those extra dashes coming in the date string. You can simply remove them using replaceAll as below.
var dt = new Date();
utc = dt.toISOString().replaceAll('-', '');
The output will be 20210422T04:45:28.739Z
You can use moment.js library which allows string format to specify. It also supports lot more operations on dates.
var dt = moment();
console.log(dt.utc().format("YYYYMMDD[T]HHmmss[Z]"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
With your explanation, I think you want the date and time to be properly displayed in utc format right?
I think using toLocaleString() method will give you what you want.
var dt = new Date();
utc = dt.toLocaleString();
One approach using plain javascript would be
var dt = new Date();
function format(num) {
return num < 10 ? "0" + num : num;
}
console.log("" + dt.getUTCFullYear() + format(dt.getUTCMonth()) + format(dt.getUTCDate()) + "T" + format(dt.getUTCHours()) + format(dt.getUTCMinutes()) + format(dt.getUTCSeconds()) + "Z")
var dt = new Date();
dt.setMilliseconds(0);
utc = dt.toISOString().replace(/[-,.,:]/g,"").replace(/000Z/, "Z");
console.log(utc)
Here is my 2 date
var startdate = '11-12-2016';
var stopdate = '13-12-2016';
I want to loop between these two dates. So, i did like this
var startMedicine = new Date(startdate);
var stopMedicine = new Date(stopdate);
while(startMedicine <= stopMedicine){
console.log(startdate)
}
But i am getting unlimited loops running in browser.
How can i do this.
Note :
I don't want to use jQuery for this one.
If the start and end date is same it should loop only once and the input date will be always d/m/y format. What is the mistake in my code. Pls help
Update :
I have mistaken the date format, my date format is d-m-y. How can i do this for one..
Increment date by one day per iteration using getDate
startdateArr = startdate.split('-');
stopdateArr = stopdate.split('-');
var startMedicine = new Date(startdateArr[2],startdateArr[1]-1,startdateArr[0]);
var stopMedicine = new Date(stopdateArr[2],stopdateArr[1]-1,stopdateArr[0]);
// thanks RobG for correcting on month index
while(startMedicine <= stopMedicine){
var v = startMedicine.getDate() + '-' + (startMedicine.getMonth() + 1) + '-' + startMedicine.getFullYear();
console.log(v);
startMedicine.setDate(startMedicine.getDate()+1);
}
In js month indexing starts at 0 so nov is 10 dec. is 11 and like so that's why i use getMonth() + 1
`
main problem is that you are not increasing your date.
here is the solution
var startdate = '11/12/2016';
var stopdate = '11/13/2016';
var startMedicine = new Date(startdate);
var stopMedicine = new Date(stopdate);
var currentMedicine = startMedicine;
var dayCount = 0;
while(currentMedicine < stopMedicine){
currentMedicine.setDate(startMedicine.getDate() + dayCount);
// You can replace '/' to '-' this if you want to have dd-mm-yyyy instead of dd/mm/yyy
var currentDate = currentMedicine.getDate() + '/' + (currentMedicine.getMonth() + 1) + '/' + currentMedicine.getFullYear(); // in dd/mm/yyyy format
console.log(currentDate);
dayCount++;
}
You can make use of moment js and moment js duration. Its for duration purpose only. It very easy and meant for same.
I need to get the date one day after another date.
I do :
$scope.date2.setDate($scope.date1.getDate()+1);
if
$scope.date1 = 2015-11-27
then
$scope.date2 = 2015-11-28
It s ok,
but when
$scope.date1 = 2015-12-02
then
$scope.date2 = 2015-11-28 (ie tomorrow)
I don't understand why...
If anyone knows..
try this instead efficient simple pure JS
var todayDate = new Date();
console.log(new Date().setDate(todayDate.getDate()+1));
so you will have that same Date type object and hence you don't need to go with moment.js
Use moment.js for this momentjs
var startdate = "2015-12-02";
var new_date = moment(startdate, "YYYY-MM-DD").add('days', 1);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(new_date);
alert(day + '.' + month + '.' + year);
In my ajax success I am getting result.Date as "/Date(-2208967200000)/". I need to check with the following date and proceed..
How to convert the "/Date(-2208967200000)/" to "01-01-1900" for below if condition?
if (result.Date != "01-01-1900") {
....
}
You can convert result.Date into you comparison date format, same as below example
var dateString = "\/Date(-2208967200000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
After doing this.. you can compare it with other date..
Reference
var jsonDate = "/Date(-2208967200000)/";
var date = new Date(parseInt(jsonDate.substr(6)));
alert(date);
The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.
jQuery dateFormat is a separate plugin. You need to load that explicitly using a tag.
You could use a regex to get the value between the brackets and then pass that to the Date():
var input = "/Date(-2208967200000)/";
var matches = /\(([^)]+)\)/.exec(input);
var date = new Date(parseInt(matches[1], 10));
Example fiddle
How i could convert datetime 5/8/2011 12:00:00 AM (m/d/yyyy) to dd-MMM-yyyy like 08-May-2011 in javascript.
This link is a good resource you can use for.
http://blog.stevenlevithan.com/archives/date-time-format
Alternatively, you need to get the individual part and concatenate them as needed like below.
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var monthnumber = now.getMonth();
var monthday = now.getDate();
var year = now.getYear();
String myOutput = monthday + "-" + monthnumer + "-" + year;
To get the month name instead of month number, you need to define an array like below
var arrMonths = new Array ("Jan","Feb"....};
String myOutput = monthday + "-" + arrMonths[monthnumer-1] + "-" + year;
check below link hope you got some idea
http://bytes.com/topic/javascript/answers/519332-how-convert-datetime-format-using-javascript
http://blog.stevenlevithan.com/archives/date-time-format
similar question solution