I have date and time format in this form:
1628489237000
I want in the below format:
DD/MM/YYYY and time.
I want to use that in javascript.
Just like this for date:
new Date(1628489237000).toLocaleDateString()
or for time:
new Date(1628489237000).toLocaleTimeString()
If you can use library, I would like to recommend you dayjs
with dayjs,
dayjs().format('DD/MM/YYYY') // 10/08/2021'
var timestamp = 1607110465663;
var date = new Date(timestamp);
var month=''+date.getMonth();
var day=''+date.getDate();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
console.log("Date: "+day+
"/"+month+
"/"+date.getFullYear()+
" "+date.getHours()+
":"+date.getMinutes()+
":"+date.getSeconds());
if yout want to time to hh/mm/ss you can convert with condition "length"
There is also a solution that uses momentjs library:
console.log(moment(1628489237000).format('DD/MM/YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
DateTime.Now.ToString("MM/dd/yyyy")
No need for any library for your case, especially not moment.js. You can read more about the project status here
var timestamp = 1628489237000;
// Create date from timestamp
var date = new Date(timestamp);
// Format date
var formattedDate = date.toLocaleDateString('fr');
var formattedTime = date.toLocaleTimeString('fr');
// Print results
console.log(formattedDate); // "09/08/2021"
console.log(formattedTime); // "8:07:17 AM"
console.log(`${formattedDate} ${formattedTime}`); // "09/08/2021 8:07:17 AM"
Related
Is there a simple way to convert standard JavaScript date format to XS:date Time
So I have a date value (new Date()) and I need in the format: 2021-06-04T13:36:00.000+05:00
It strange but could not find simple solution
I think this worked for you
var date = new Date();
var formattedDate = date.toISOString() + "+" +(date.getTimezoneOffset() / 60) + ":00"
console.log(formattedDate)
I'm receiving date and time from the REST API in the below format
2016-01-17T:08:44:29+0100
I want to format this date and time stamp like
17-01-2016 08:44:29
It should be dd/mm/yyyy hh:mm:ss
How to format this in TypeScript?
you can use moment.js. install moment js in your project
moment("2016-01-17T:08:44:29+0100").format('MM/DD/YYYY');
for more format option check Moment.format()
Have a look at this answer
You can create a new Date("2016-01-17T08:44:29+0100") //removed a colon object and then get the month, day, year, hours, minutes, and seconds by extracting them from the Date object, and then create your string. See snippet:
const date = new Date("2016-01-17T08:44:29+0100"); // had to remove the colon (:) after the T in order to make it work
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
const minutes = date.getMinutes();
const hours = date.getHours();
const seconds = date.getSeconds();
const myFormattedDate = day+"-"+(monthIndex+1)+"-"+year+" "+ hours+":"+minutes+":"+seconds;
document.getElementById("dateExample").innerHTML = myFormattedDate
<p id="dateExample"></p>
It is not the most elegant way, but it works.
Check if this is helpful.
var reTime = /(\d+\-\d+\-\d+)\D\:(\d+\:\d+\:\d+).+/;
var originalTime = '2016-01-17T:08:44:29+0100';
var newTime = originalTime.replace(this.reTime, '$1 $2');
console.log('newTime:', newTime);
Output:
newTime: 2016-01-17 08:44:29
new Date().toLocaleString()
output:
"31/03/2020 ,00:00:0"
(now,your output is string)
I have the following code which I get from parameters in the URL.
This is what I have in the URL
&dateStart=15.01.2015&timeStart=08%3A00&
After getting the parameters I have the following: 15.01.2015:08:00
Using Javascript how can I parse this string to get the date in milliseconds?
Date.parse(15.01.2015:08:00)
But obviously this doesn't work.
Date.parse(15-01-2015)
This works and I can change this but then how do I add or get the milliseconds from the time??
This is quite possibly the ugliest JavaScript function I've written in my life but it should work for you.
function millisecondsFromMyDateTime(dateTime) {
var dayMonth = dateTime.split('.');
var yearHourMinute = dayMonth[2].split(':');
var year = yearHourMinute[0];
var month = parseInt(dayMonth[1]) - 1;
var day = dayMonth[0];
var hour = yearHourMinute[1];
var minute = yearHourMinute[2];
var dateTimeObj = new Date(year, month, day, hour, minute, 0, 0);
return dateTimeObj.getTime();
}
It will work with the format that your DateTime is in aka day.month.year:hours:minutes.
You can achieve it using Javascript Date Object and JavaScript getTime() Method:
var dateString="01.15.2015 08:00";
var d = new Date(dateString);
console.log(d);
var ms=d.getTime();
console.log(ms);
ms+=10000;
console.log(new Date(ms));
Here is a DEMO Fiddle.
Note: Change your date string from 15.01.2015:08:00 to "01.15.2015 08:00" because it's not a valid Date format.
Check for format
Date() in javascript :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Format allowed :
https://www.rfc-editor.org/rfc/rfc2822#page-14
You can try to use moment.js library like this:
moment('15.01.2015 08:00', 'DD.MM.YYYY HH:mm').milliseconds()
Just for the sake of completion, you can always extract the information and create a Date object from the extracted data.
var dateStart = '15.01.2015'
var timeStart = '08:00';
var year = dateStart.substring(6,10);
var month = dateStart.substring(3,5);
var day = dateStart.substring(0,2);
var hour = timeStart.substring(0,2);
var mins = timeStart.substring(3,5);
var fulldate = new Date(year, month-1, day, hour, mins);
console.log(fulldate.getTime());
i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.
here my javascript Code:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);
alert(new_date);
here my jsfiddle: http://jsfiddle.net/jbgUt/1/
How can i solve this ?
I like this string format "25.03.2014"
Hope someone can help me.
UPDATED: January 19, 2016
As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)
var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
Thanks #Bala for the information.
UPDATED: March 21, 2014
This is what you'd have to do to get that format.
Here's an updated fiddle
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(day + '.' + month + '.' + year);
ORIGINAL: March 20, 2014
You're not telling it how/what unit to add. Use -
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))
has to format and then convert to moment again.
The function add() returns the old date, but changes the original date :)
startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
You can add days in different formats:
// Normal adding
moment().add(7, 'days');
// Short Hand
moment().add(7, 'd');
// Literal Object
moment().add({days:7, months:1});
See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/
var end_date = moment(start_date).clone().add(5, 'days');
If we want to use the current date or present date:
var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);
To get an actual working example going that returns what one would expect:
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
add https://momentjs.com/downloads/moment-with-locales.js to your html page
var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date
then
var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..
point 2 and 3 are using in your jquery code...
If you do end up running with formatting problems after adding X time to the function, try this format:
startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");
instead of:
startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");
This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.
You can reduce what they said in a few lines of code:
var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
updated:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
alert(new_date)
I have a string 10/11/2012 meaning November 10, 2012.
But when I do new Date("10/11/2012") it returns October 11th.
How do I pass in the date format I want? In this case dd-mm-yyyy
I found jQuery.datepicker.parseDate(format, Date) at this site:
http://docs.jquery.com/UI/Datepicker/$.datepicker.parseDate
So I will be using the jQuery datepicker instead.
Unfortunately, there's no JavaScript Date constructor that allows you to pass in culture information so that it uses localized date formats. Your best bet is to use the constructor that takes the year, month, and day separately:
var parts = dateString.split('/');
var date = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10),
parseInt(parts[0], 10));
For this specific case, you can use:
var dateparts = date.split("/");
var datestring = dateparts[1] + "/" + dateparts[0] + "/" + dateparts[2];
var date = new Date(datestring);
In the more general case, you can extend the Date prototype, as demonstrated in this answer:
https://stackoverflow.com/a/13163314/1726343