How to change date format using jQuery? - javascript

I have a date in a format like this fecha2.value = '2014-01-06', but I want to change the format to this '01-06-14' using jQuery.
How can I do this?

You can use date.js to achieve this:
var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');
Alternatively, you can do it natively like this:
var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);
console.log(newDate);

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
curr_year = curr_year.toString().substr(2,2);
document.write(curr_date+"-"+curr_month+"-"+curr_year);
You can change this as your need..

You don't need any date-specific functions for this, it's just string manipulation:
var parts = fecha2.value.split('-');
var newdate = parts[1]+'-'+parts[2]+'-'+(parseInt(parts[0], 10)%100);

I dont think you need to use jQuery at all, just simple JavaScript...
Save the date as a string:
dte = fecha.value;//2014-01-06
Split the string to get the day, month & year values...
dteSplit = dte.split("-");
yr = dteSplit[0][2] + dteSplit[0][3]; //special yr format, take last 2 digits
month = dteSplit[1];
day = dteSplit[2];
Rejoin into final date string:
finalDate = month+"-"+day+"-"+year

In most projects you want to use moment.js library along with datepicker and datetimepickers .
It doesnt look that verbose. For example
let dateString_ = moment('2023-01-17T13:39:48.530801Z').format("YYYY-MM-DD HH:mm");
for 2023-01-17 18:39
Formatting rules are here

Related

How to change format to standard time JavaScript

I have a date string in JavaScript which is arranged in a particular manner. How do I rearrange this in order to fit standard time?
let date = "2020-06-01T00:00:00Z"
How do I rearrange the date variable in order to match the format MM/DD, YYYY ?
Change your code to as follows:
let date = "2020-06-01T00:00:00Z";
date = new Date(date);
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
console.log(mm+'/'+dd+', '+yyyy)
i don't real know this is possible but i found the function you can use instead
function GetFormattedDate() {
var todayTime = new Date();
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
var year = todayTime.getFullYear();
return month + "/" + day + "/" + year;
}
console.log(GetFormattedDate());
so you can add more in the function or edit what the function will return as a format according to your will

How can I convert timestamp to date format yyyy-MM-dd'T'HH:mm:ssXXX?

I would like to know if there's a way to convert a timestamp to yyyy-MM-dd'T'HH:mm:ssXXX date format?
I can convert it to ISO using toISOString but it add Z at the end of the string.
Thank you.
var d = new Date();
var datestring = d.getDate() + "-" +
(d.getMonth() + 1) + "-" +
d.getFullYear() + "-T " +
d.getHours() + ":" +
d.getMinutes();
If you really do not want to use an external library like moment.js (which i would strongly recommend), as you stated in your comment, you will have to implement a function for that yourself, as regular javascript does not provide a function for this (as far as i know).
You can create an object of javascripts built-in Date class from a unix timestamp by using
var unixTimestamp = 1566394163;
//multiply with 1000 as Date expects milliseconds
var date = new Date(unixTimestamp * 1000);
Then you could build the output string yourself, by using something along this
var dateString = "";
dateString += date.getUTCFullYear()+"-";
dateString += date.getUTCMonth()+"-";
dateString += ...
On the other hand, if the Z at the end of the string is the only thing that bothers you about the format provided by toISOString() as a workaround you could use its output and remove the last character of it
var dateString = date.toISOString();
var newDateString = dateString.substr(0, dateString.length -1);
Please try using below code to convert timestamp to date format.
var date = new Date(timestamp*1000);
var year = date.getFullYear();
var month = months_arr[date.getMonth()];
var day = date.getDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
Display date time in any format you want.

convert string to Date Javascript

Trying to convert a string to Javascript Date.
My string:
var fullDate = this.date + " " + this.time + ":00";
gives an output: 24/12/2016 02:00:00
but trying to convert it in js Date object like:
var k = new Date(fullDate);
gives the output: Invalid Date
or even: var k = Date.parse(fullDate);
gives the output: NaN
Any help is welcome.
I was following instructions from http://www.datejs.com/ so far
Your format is invalid. Valid format is month/day/year, so try:
var fullDate = "12/24/2016 02:00:00";
var k = new Date(fullDate);
Hope I could help!
I've just posted an answer/question for this.
Here's my answer
In summary you need to format the string to a standardized format first and then you can use that string with the new Date(string) method. This work for many browsers (IE9+, Chrome and Firefox).
stringFormat = moment(dateObject).format("YYYY-MM-DDTHH:mm:ss");
date = new Date(stringFormat);
That's happening because you don't provide a valid date format for the new Date. So the date format is incorrect. The default date format in JavaScript is dd/mm/yyyy.
The date should be like(mm/dd/yyyy):
12/24/2015
And then you can use var k = new Date(fullDate);
A pure javascript solution to format the date(without moment.js):
var fullDate = "24/12/2016 02:00:00";
fullDate = fullDate.split(' ');
var date = fullDate[0].split(/\//);
var time = fullDate[1];
var newDate = date[1] + '/' + date[0] + '/' + date[2] + ' ' + time;
var k = new Date(newDate);

Get clientside Date and Time format in Javascript

I want is, the formatted string as, MM/DD/YY or DD/MM/YYYY.
Which of these is set by client in his/her system.
How to get it?
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var final = day + "/" + month + "/" + year;
console.log(final); // "1/11/2015"
http://jsbin.com/zixacacuda/edit?js,console
var dt=new Date('2015-12-12');
var d=dt.getDay();
var m=dt.getMonth();
var y=dt.getFullYear();
var dtformated=d+'/'+m+'/'+y
console.log(dtformated)
var now = new Date().toLocaleDateString();
it willl retrun "12/28/2015 (MM/DD/YYYY)" format.

javascript date increment

I want to set a text box with a date (in dd/mm/yyyy format) 14 days ahead to current date in javascript . can any one help me regarding this ?
This should do it:
var myDate=new Date();
myDate.setDate(myDate.getDate()+14);
then
document.getElementById(YOUR_TEXTBOX_ID).value = myDate.getDate() + "/" +
(myDate.getMonth() + 1) + "/" + myDate.getFullYear();
Date.js is a handy script for all kinds of JavaScript date manipulation. I've used it to make many date-based interfaces, including calendar controls.
Like Deodeus suggested, use Date.js:
var myDate = Date.today().add(14).days();
document.getElementById('mytextbox').value = myDate.toString('dd/MM/yyyy');
Following is the function to increment date by one day in javascript.
function IncrementDate(date) {
var tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + 1);
return tempDate;
}
Function calling...
var currentDate = new Date();
var IncrementedDate = IncrementDate(currentDate);

Categories

Resources